【问题标题】:How to save an camera photo with an image over top in Swift 3如何在 Swift 3 中保存带有顶部图像的相机照片
【发布时间】:2016-10-28 02:23:07
【问题描述】:

在我的主视图中,我有一个 UIView,其中有一个 imageView,相机预览被拉入其中,第二个 imageView 基本上是一个图形边框,覆盖相机预览。

当用户点击“保存”按钮时,我很难找到保存这两个图像的支持。

到目前为止,这是我的代码:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var pickedImage: UIImageView!
@IBOutlet weak var photoFrame: UIImageView!
@IBOutlet weak var photoWrapper: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction func saveAction(_ sender: UIButton) {
    let imageData = UIImageJPEGRepresentation(pickedImage.image!, 0.99)
    let compressedImage = UIImage(data: imageData!)
    UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)
}

@IBAction func cameraAction(_ sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
}

@IBAction func libraryAction(_ sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    pickedImage.image = image
    self.dismiss(animated: true, completion: nil)
}
}

现在,点击“cameraAction”按钮将显示相机,并允许您拍照,当您点击“使用此照片”时,会将拍摄的照片加载到“pickedImage”图像视图中。

理想情况下,我希望我的相框叠加层也出现在相机视图中 - 但是 - 我们将一次解决一个问题(除非阅读本文的人知道在同一个答案中实现这一点的方法) .

然而,我主要关心的是获取它,以便 saveAction 可以将照片和叠加图像捕获为一个。现在,我可以看到代码从照片视图中创建了一个 UIImageJPEGRepresentation ......所以我想最大的问题是 - 我是否能够在其中添加第二张图像(我猜不是),或者,做我必须使用其他方法将两张照片合二为一,然后调用 UIImageJPEGRepresentation?

【问题讨论】:

    标签: ios xcode camera swift3 photo


    【解决方案1】:

    在操场上编写了这个函数,它可以将一个图像叠加在另一个图像之上。您可以在生成的合成图像上调用 UIImageJPEGRepresentation。

        func composite(image:UIImage, overlay:(UIImage), scaleOverlay: Bool = false)->UIImage?{
            UIGraphicsBeginImageContext(image.size)
            var rect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
            image.draw(in: rect)
            if scaleOverlay == false {
                rect = CGRect(x: 0, y: 0, width: overlay.size.width, height: overlay.size.height)
            }
            overlay.draw(in: rect)
            return UIGraphicsGetImageFromCurrentImageContext()
        }
    

    【讨论】:

    • 这几乎可以工作 - 除了叠加照片非常小,而且是颠倒和颠倒的。知道发生了什么吗?
    • 编辑了源代码以添加可选的缩放参数并更改为 UIKit 方法;我忘记了 CoreGraphics 在 iOS 上相对于 Mac 是颠倒的。请注意,您应该使用与相机 (4:3) 相同纵横比的更大叠加层,否则在缩放时会显得很奇怪。
    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 2017-02-10
    • 2011-12-04
    • 2017-07-14
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多