2013-01-09 23 views
0

iam做一個應用程序。在那個Im使用一個imageview與圖像和添加一個視圖與sone清除holes.Means通過這些洞我們可以看到背景image.So我的問題是我想捕獲總屏幕(imageview與這個洞視圖).Iam使用下面的代碼,但它不工作。捕獲屏幕,因爲它看起來像

- (UIImage*)captureView:(UIView *)yourView { 
CGRect rect = [[UIScreen mainScreen] bounds]; 
UIGraphicsBeginImageContext(rect.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
[yourView.layer renderInContext:context]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return image; 
} 

但它不工作。

+0

你正在傳遞yourView什麼? –

回答

0

這是我們如何做到:

UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, yourView.opaque, 0.0); 
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *LastImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
0

試試這個把你screeShoT

-(UIImage*)getScreenShot 
{ 
    CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size; 

    //CGSize screenSize = CGSizeMake(1024, 768); 
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 

    CGContextRef ctx = CGBitmapContextCreate(nil, screenSize.width, 416, 8, 4*(int)screenSize.width, colorSpaceRef, kCGImageAlphaPremultipliedLast); 
    CGContextTranslateCTM(ctx, 0.0, screenSize.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 

    [(CALayer*)self.yourView.layer renderInContext:ctx]; 

    CGImageRef cgImage = CGBitmapContextCreateImage(ctx); 
    UIImage *image = [UIImage imageWithCGImage:cgImage]; 
    CGImageRelease(cgImage); 
    CGContextRelease(ctx); 

    return image; 

}