如何捕捉UIView顶部UIView

我有显示graphics的UIView ..现在我需要将其捕获到UIImage ,我GOOGLE了它,并得到了下面的code.but如果我在我的代码中使用它不工作。即使我使用断点编译器没有达到这个。我在我的UIView使用这个,我错了?

 + (UIImage *) imageWithView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } 

你可以用下面的方法截图iPhone应用程序的任何视图或整个屏幕

 - (UIImage *)captureView { //hide controls if needed CGRect rect = [self.view bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [self.view.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } 

称之为波纹pipe

 UIImage *tempImageSave=[self captureView]; 

你也可以保存这个图像与此波纹线相册。

 UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil); 

并且您也可以将此图像与此文档目录的波纹线一起保存。

 NSData *imageData = UIImagePNGRepresentation(tempImageSave); NSFileManager *fileMan = [NSFileManager defaultManager]; NSString *fileName = [NSString stringWithFormat:@"%d.png",1]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; [fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil]; 

如果视图包含图层图像或一些graphics相关的数据,则使用下面的方法。

 -(UIImage *)convertViewToImage:(UIView *)viewTemp { UIGraphicsBeginImageContext(viewTemp.bounds.size); [viewTemp drawViewHierarchyInRect:viewTemp.bounds afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

并使用下面的方法。

 UIImage *tempImageSave = [self convertViewToImage:yourView]; 

我希望这对你有帮助。

试试这个代码可能会帮助你

 - (UIImage *)captureView:(UIView *)view { CGRect screenRect = [[UIScreen mainScreen] bounds]; UIGraphicsBeginImageContext(screenRect.size); CGContextRef ctx = UIGraphicsGetCurrentContext(); [[UIColor clearColor] set]; CGContextFillRect(ctx, screenRect); [view.layer renderInContext:ctx]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

您可以像这样保存视图的图像,并将图像存储到文档目录中: –

 -(void)SaveViewAsImage { UIGraphicsBeginImageContext(self.view.bounds.size); UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext(); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIGraphicsEndImageContext(); NSData *imageData = UIImagePNGRepresentation(saveImage); NSFileManager *fileMan = [NSFileManager defaultManager]; NSString *fileName = [NSString stringWithFormat:@"%d.png",1]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; [fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil]; } 

您的图像保存在文档目录:)

下载这个演示

http://www.sendspace.com/file/gjxa82