Ios 是否将imageView、图像和背景图像保存到camera roll?
在我的视图控制器中,我有一个imageview,下面有一个textview,还有一个背景图像 我想把所有这些都拍摄成一幅图像,并将其发送到摄像机上 如果有办法,你怎么做?Ios 是否将imageView、图像和背景图像保存到camera roll?,ios,xcode,image,uiimageview,uitextview,Ios,Xcode,Image,Uiimageview,Uitextview,在我的视图控制器中,我有一个imageview,下面有一个textview,还有一个背景图像 我想把所有这些都拍摄成一幅图像,并将其发送到摄像机上 如果有办法,你怎么做?#导入 #import <QuartzCore/QuartzCore.h> - (void) imageWithView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
#导入
#import <QuartzCore/QuartzCore.h>
- (void) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}
-(无效)图像WithView:(UIView*)视图
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size,view.不透明,0.0);
[view.layer RenderContext:UIGraphicsGetCurrentContext()];
UIImage*img=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
}
此代码函数将从您要传递的视图创建一个图像,例如,如果您给它self.view,它将使用self.view上的每个元素创建一个图像,然后将其保存到相册中,Igor的回答是正确的,但renderContext相当慢。用更现代的(iOS 7+)API替换:
[view drawViewHierarchyInRect:[UIScreen mainScreen]界限]屏幕更新后:否];
在我的测试中,该方法快了50%以上。您的所有图像和文本视图都放在不同的UIView中,并为此视图设置出口,然后在视图控制器中编写此代码
- (UIImage *) screenshot {
UIGraphicsBeginImageContextWithOptions(self.saveview.bounds.size, NO, 1.0f);
[self.saveview drawViewHierarchyInRect:self.saveview.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;}
将此saveview替换为您的UIView名称
这种方法的使用是
UIImage *sourceImage=[self screenshot];
然后像那样保存到你的相机卷上
UIImageWriteToSavedPhotosAlbum(sourceImage, nil, nil, nil);
希望这对您有所帮助。除了使用UIGraphics,还有其他方法吗。。。?因为,我得到了几个错误:CGContextSaveGState:invalid context 0x0错误,与assetsd的连接被中断或assetsd diedIs除了使用UIGraphics还有其他方法吗。。。?因为,我遇到了两个错误:CGContextSaveGState:invalid context 0x0错误,与assetsd的连接中断或assetsd断开,这通常意味着您的上下文为空或其大小尚未设置。你也可以在你的视图中执行
snapshotForView:inRect
,但它返回的是UIView而不是UIImage。先生,你得到了我的支持票,因为我不知道这一点谢谢@Igor.Mursa。几天前,当我需要提高应用程序的性能时,我才发现了这一点。