2013-03-06 50 views
0

我想要在iOS上獲得UIView屏幕捕獲。iOS使用UIImageView捕獲UIView裏面

在我看來,我有兩個UIImageView動畫。

,我使用它來捕捉視野中的代碼是:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1.0); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
// Read the UIImage object 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

UIImageWriteToSavedPhotosAlbum(image, self, 
           @selector(image:didFinishSavingWithError:contextInfo:), nil); 

結果是與背景的圖像和僅一個的UIImageView。兩個ImageView都一直在移動。如果我拍了幾張照片,那麼UIImageView每次都處於相同的位置。

+1

使用self.view.layer.presentationLayer使用下面的行 – Till 2013-03-06 17:42:27

回答

2

試試這個:

-(CGImageRef)imageCapture 
{ 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    CGRect rect= CGRectMake(0,0 ,width, height); 

    CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect); 
    return imageRef; 
} 

,每當你想捕捉的屏幕

UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];