[英]How can I save a PHLivePhoto to photo library?
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). 我是Swift的新手,我正在开发一个应用程序,该程序从库中选择一个实时照片,并将其显示在imageview上,直到该部分起作用为止,但是该imageview位于另一个具有自定义图像(例如背景)的imageview的顶部图片)。 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. 我有一个以银河系图像为背景的imageview,在它上面有一个显示实时照片的imageview,所以我想将两者都存储到库中,当我在库中检查它时,我将拥有一个新的live照片有一个银河系的背景。
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 **这是例子
You need to draw the selectedImage
on top of the backgroundImage
, and save the result. 您需要在
backgroundImage
顶部绘制selectedImage
,然后保存结果。
Add this extension of UIImage
添加此扩展的
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. 其中
backgroundImage
是自定义背景, userImage
是从用户库中选择的实时照片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.