【问题标题】:Saving an image displayed in a UIImageView保存在 UIImageView 中显示的图像
【发布时间】:2014-10-04 19:46:13
【问题描述】:

我有一组显示在 UICollectionView 中的图像。

当按下集合视图中的单元格时,该图像被推送到视图控制器并显示在 UIImageView 中。

然后我希望能够按下按钮并将图像保存到用户的相机胶卷。

但我在这样做时遇到了一些麻烦......

我认为我的代码是正确的,但无法让它们一起工作:

- (IBAction)onClickSavePhoto:(id)sender{

     UIImage *img = [UIImage imageNamed:@"which ever image is being currently displayed in the image view"];

     UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}

如何操作代码以允许用户保存图像视图中显示的图像?

提前致谢!

更新:

在另一个帖子中找到了问题的解决方案:

Save image in UIImageView to iPad Photos Library

【问题讨论】:

    标签: ios objective-c uiimageview uiimage


    【解决方案1】:

    如何将图像保存到库中:

    你可以使用这个功能:

    UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                                   id completionTarget, 
                                   SEL completionSelector, 
                                   void *contextInfo);
    

    你只需要completionTargetcompletionSelectorcontextInfo,如果你想在UIImage完成保存时得到通知,否则你可以传入nil

    More info here

    可能比使用 UIImageWriteToSavedPhotosAlbum 更快地将图像保存到库中: 使用 iOS 4.0+ AVFoundation 框架的方式比 UIImageWriteToSavedPhotosAlbum 快得多

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) { // TODO: error handling } 
        else { // TODO: success handling }
    }];
    
    //for non-arc projects
    //[library release];
    

    获取 UIImageView 中任何内容的图像作为屏幕截图:

    iOS 7 有一个新方法,允许您将视图层次结构绘制到当前图形上下文中。这可用于非常快速地获取 UIImage。

    这是 UIView 上的一个类别方法,用于将视图作为 UIImage 获取:

    - (UIImage *)takeSnapShot {
        UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, NO, [UIScreen mainScreen].scale);
    
        [self drawViewHierarchyInRect:self.myImageView.bounds afterScreenUpdates:YES];
    
        // old style [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    它比现有的 renderInContext: 方法快得多。

    参考:https://developer.apple.com/library/ios/qa/qa1817/_index.html

    为 SWIFT 更新:具有相同功能的扩展:

    extension UIView {
    
        func takeSnapshot() -> UIImage {
            UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, false, UIScreen.mainScreen().scale);
    
            self.drawViewHierarchyInRect(self.myImageView.bounds, afterScreenUpdates: true)
    
            // old style: self.layer.renderInContext(UIGraphicsGetCurrentContext())
    
            let image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return image;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多