【问题标题】:Saving an imageView, image, and a background image to camera roll?将 imageView、图像和背景图像保存到相机胶卷?
【发布时间】:2015-02-16 14:42:28
【问题描述】:

在我的视图控制器中,我有一个 imageview、一个 textview 下面和一个背景图像?

我想将所有这些作为图像捕获,并将其发送到相机胶卷

如果有办法,你是怎么做到的?

【问题讨论】:

    标签: ios xcode image uiimageview uitextview


    【解决方案1】:

    您的所有图像和文本视图放入不同的 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;}
    

    用您的 UIView 名称替换此 saveview

    使用这个方法是..

        UIImage *sourceImage=[self screenshot];
    

    然后像这样保存到你的相机胶卷..

       UIImageWriteToSavedPhotosAlbum(sourceImage, nil, nil, nil);
    

    希望这对你有帮助..

    【讨论】:

      【解决方案2】:

      Igor 的回答是正确的,但 renderInContext 很慢。将其替换为更现代的 (iOS 7 +) API:

      [view drawViewHierarchyInRect:[[UIScreen mainScreen] bounds] afterScreenUpdates:NO];

      在我的测试中,该方法的速度提高了 50% 以上。

      【讨论】:

      • 除了使用 UIGraphics 之外还有其他方法吗...?因为,我收到了几个错误:CGContextSaveGState: invalid context 0x0 error, and Connection to assetsd was interrupted or assetsd dead
      • 这通常意味着您的上下文为 NULL 或者它的大小尚未设置。您也可以在视图上执行snapshotForView:inRect,但它返回的是 UIView 而不是 UIImage。
      • 先生,你得到了我的支持,因为我不知道这个
      • 谢谢@Igor.Mursa。几天前,当我需要提高应用程序的性能时,我才发现它。
      【解决方案3】:
      #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);
      
      }
      

      此代码功能将从您将传递的视图中为您创建一个图像,例如,如果您给它 self.view 它将创建一个包含 self.view 上的每个元素的图像,然后将其保存到相册,

      【讨论】:

      • 除了使用 UIGraphics 之外还有其他方法吗...?因为,我收到了几个错误:CGContextSaveGState: invalid context 0x0 error, and Connection to assetsd was interrupted or assetsd dead
      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多