0

I am new in Swift and I am developing an app that picks a live photo from the library and displays it on an imageview, until that part it works, but the imageview is on top of another imageview that has a custom image(like a background image). By the moment when I hit the save button it only stores the image view with the custom background. I am using the following code to store it on the library:

let imageData = UIImagePNGRepresentation(myView.image!)
let compressedImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedImage!, nil, nil, nil)

This code is of course on the save button.

what I want to do is to give the user that ability to store both things at the same time, like for example:

I have an imageview with an image of a galaxy as background and on top of it I have an imageview displaying a live photo, so I want to store both things to the library and when I check it on the library I will have a new live photo with a background of a galaxy.

I don't know if somebody will understand the example, but is I think it is the best I can do to describe the problem.

** this is the example

enter image description here

Nishant Bhindi
  • 2,242
  • 8
  • 21

1 Answers1

0

You need to draw the selectedImage on top of the backgroundImage, and save the result.

Add this extension of UIImage

extension UIImage {
    func saveImage(withImage image: UIImage) -> UIImage? {
        // create image
        UIGraphicsBeginImageContext(size)
        draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let centeredFrame = CGRect(x: (size.width - image.size.width)/2, y: (size.height - image.size.height)/2, width: image.size.width, height: image.size.height)
        image.draw(in: centeredFrame)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
}

And use it

if let newImage = backgroundImage.saveImage(withImage: userImage), let imageData = UIImagePNGRepresentation(newImage), let compressedImage = UIImage(data: imageData) {
    UIImageWriteToSavedPhotosAlbum(compressedImage, nil, nil, nil)
}

Where backgroundImage is custom background and userImage is live photo selected from user library.

Jože Ws
  • 1,754
  • 17
  • 12
  • Thanks for replying! I am having some problems trying to add that code to my code :( – Freddy Manzanares May 28 '17 at 18:45
  • whats the problem? – Jože Ws May 28 '17 at 18:46
  • I guess I have to change the "backgroundImage" to what It is the piece of code that is refering to my background right? like to the outlet? The thing is that I change it and it seems not to have a member for "size" – Freddy Manzanares May 28 '17 at 23:18
  • i have added to my question a foto, can you please check it? – Freddy Manzanares May 28 '17 at 23:37
  • backgroundImage is `UIImage` type and it has a [size property](https://developer.apple.com/reference/uikit/uiimage/1624105-size), do not confuse with `UIImageView`. Have you tried the code? – Jože Ws May 29 '17 at 13:12
  • well, I made a new outlet for the UIImage, and I have added the code but I am having troubles with the "draw" from the second line: draw(in: CGRect(x: 0, y: 0, width: ImageView.size.width, height: ImageView.size.height)) the error message says : use of unresolved identifier "draw" did you saw the photo I added to my question? – Freddy Manzanares May 30 '17 at 00:38
  • I updated my answer with a compiling version, let me know how it works. – Jože Ws May 30 '17 at 11:24
  • i have added the code but now i am having troubles with the "userImage" as it is a PHLivephotoView it throws me: "Cannot convert value of type 'PHLivephotoView!' to expected argument type 'UIImage'". – Freddy Manzanares May 31 '17 at 00:43