Ios 从视图中捕获图像
我使用以下代码将UIView捕获为图像Ios 从视图中捕获图像,ios,alassetslibrary,uigraphicscontext,Ios,Alassetslibrary,Uigraphicscontext,我使用以下代码将UIView捕获为图像 -(void)startSnapSot { NSLog(@"TakeSnapShot"); UIImage* image = nil; // image = [self screenshot]; UIGraphicsBeginImageContext(overlayView.frame.size); { [overlayView.layer render
-(void)startSnapSot {
NSLog(@"TakeSnapShot");
UIImage* image = nil;
// image = [self screenshot];
UIGraphicsBeginImageContext(overlayView.frame.size);
{
[overlayView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"Error Saving Image");
}
else {
NSLog(@"Image Saved");
}
};
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage]
orientation:(ALAssetOrientation)[image imageOrientation]
completionBlock:completionBlock];
}
但问题是,它一次又一次地保存第一幅图像,也就是说,它一次又一次地保存同一幅图像,而不是捕获不同的UIView图像。有人能告诉我我做错了什么吗?将
UIView
或CGLayer
参数添加到startSnapSot
方法中,以使执行更加清晰,您可能忘记更改overlayView
var处的引用。overlayView声明为全局。因此,我不需要将参数传递给startSnapSot方法。它将自动执行。这就是我所说的,在下次调用该方法之前,您可能不会更改其值,这就是为什么在方法中使用全局变量是一种不好的做法的原因之一。如果改用参数,它将更加安全和清晰。