iphone 截屏操作

1本文摘自:http://blog.csdn.net/arthersophy/article/details/5831682

利用下面的方法可以截取参数view所代表的界面。通常情况下,我们都能拿到view的指针。所以该方法可以截取程序界面全部或者部分UI.

显示的时候,可以利用UIImageView.

UIImage*        
ImageOfView( UIView* view ){
    // Takes a snapshot of a view and returns it, can be used for caching a view
    static BOOL timeIt = NO;
    double startm, stopm;
    struct timeval start, stop;
    if( timeIt ){
        gettimeofday( &start, nil );
        startm = start.tv_usec;
    }
    
    // Create the bitmap context
    CGColorSpaceRef        colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef            cgctx = CGBitmapContextCreate( NULL, view.frame.size.width, view.frame.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast );
    CGColorSpaceRelease(colorSpace);    
    if( !cgctx ){
        return nil;
    }
    CGContextTranslateCTM( cgctx, 0.0, view.frame.size.height );    // Have to move/flip the image to come out right-side up in the UI layer
    CGContextScaleCTM( cgctx, 1.0, -1.0);

    // render the layer into the bitmap context and get the image
    [view.layer renderInContext:cgctx];
    CGImageRef                bitmap = CGBitmapContextCreateImage( cgctx );
    CGContextRelease( cgctx );
    
    // convert the finished image to a UIImage
    UIImage*                    theImage = [[UIImage alloc] initWithCGImage:bitmap];
    CGImageRelease( bitmap );
    if( timeIt ){
        gettimeofday( &stop, nil );
        stopm = (stop.tv_sec-start.tv_sec)*1000000+stop.tv_usec;
        NSLog( @"ImageOfView %f msecs", (stopm-startm)/1000. );
    }
    return theImage;
}


posted on 2011-12-05 15:28  wtq  阅读(348)  评论(0编辑  收藏  举报