【问题标题】:How do I save a UIImageView with subviews to Camera roll?如何将带有子视图的 UIImageView 保存到相机胶卷?
【发布时间】:2016-07-15 13:19:44
【问题描述】:

我有一个应用程序,它从相机拍摄照片并将其放入 UIImageView。在您可以向其中添加小的“剪贴画”图片之后,这些图片将作为子视图添加到 UIImageView(我的代码中的 tempImageView)。

但是当我尝试通过 tempImageView.image 将图像保存到相机时,图像会变得更大,并且添加到其中的子视图也不会出现。知道如何将带有我的子视图的 UIImageView 保存到相机胶卷吗?

这是我保存图像的方法:

@IBAction func saveImageButtonPressed(sender: UIButton) {

    UIImageWriteToSavedPhotosAlbum(tempImageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)
}

func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    }
}

下面是我将图片添加到 tempImageView 的方法:

@IBAction func berlockButtonPressed(sender: UIButton) {
    let imageName = "berlock.png"
    let image = UIImage(named: imageName)
    let imageView = UIImageView(image: image!)
    imageView.frame = CGRect(x: 200, y: 200, width: 60, height: 100)

    tempImageView.addSubview(imageView)
}

感谢您的帮助。

【问题讨论】:

    标签: ios swift uiimageview


    【解决方案1】:

    您必须在图像上下文中绘制图像和图像视图的子视图,并对该上下文“拍照”。我还没有测试过这段代码,但这会让你开始:

    // Create the image context to draw in
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, UIScreen.mainScreen().scale)
    
    // Get that context
    let context = UIGraphicsGetCurrentContext()
    
    // Draw the image view in the context
    imageView.layer.renderInContext(context!)
    
    // You may or may not need to repeat the above with the imageView's subviews  
    // Then you grab the "screenshot" of the context
    let image = UIGraphicsGetImageFromCurrentImageContext()
    
    // Be sure to end the context
    UIGraphicsEndImageContext()
    
    // Finally, save the image
    UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
    

    【讨论】:

    • 非常感谢!这成功了,如果有人想知道,我也不需要为我的 tempImageViews 子视图重复。
    【解决方案2】:

    你应该像这样渲染图像,

       UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, imageView.opaque, 0.0)
    
        imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    
         let resultImageToStore = UIGraphicsGetImageFromCurrentImageContext()
    
        UIGraphicsEndImageContext()
    

    你可以给你想要的大小,而不是imageView.bounds.sizeimageView.bounds.size 保持图像视图的大小。

    imageView 视为您的 imageView,其中有另一个子视图。

    resultImageToStore 是您应该存储的最终图像。

    【讨论】:

    • 谢谢,从我所看到的情况来看,这可能也有效,但我使用了 keithbhunter 的解决方案,该解决方案更具解释性。
    猜你喜欢
    • 2022-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2016-05-01
    • 2015-01-06
    相关资源
    最近更新 更多