iOS 9 中带有 UIImageView 的 UIImageWriteToSavedPhotosAlbum
UIImageWriteToSavedPhotosAlbum with UIImageView in iOS 9
我正在尝试在我的应用程序中开发一个 "Save image" 功能,用户可以在其中将 TableView 中的图像保存在他们的照片库中。我在社区中搜索了很多,但找不到与我的案例相关的任何内容,所以我将对其进行解释:
正如官方指南中所述,UIImageWriteToSavedPhotosAlbum 必须接收一个 UIImage,因此我试图将我的 UIImageView 转换为 UIImage 以实现这一点,但是当一切正常且没有警告或错误时,APP显示错误 "Data unavailable".
我需要做什么才能让它发挥作用?我是否发送了错误的数据?
<UIImage: 0x16db7780>, {400, 345}
这是我的代码:
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil
{
let ac = UIAlertController(title: "Imagen guardada", message: "", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
else
{
let ac = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
}
@IBAction func handleGesture(sender: AnyObject) {
self.votarFrame?.hidden = true
let guardarMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
var img:UIImage!
img = self.productoImageView.image
let saveImage = UIAlertAction(title: "Guardar imagen", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
UIImageWriteToSavedPhotosAlbum(img, self, "image:didFinishSavingWithError:contextInfo:", nil)
})
let cancelAction = UIAlertAction(title: "Cancelar", style: UIAlertActionStyle.Cancel, handler: nil)
guardarMenu.addAction(saveImage)
guardarMenu.addAction(cancelAction)
self.presentViewController(guardarMenu, animated: true, completion: nil)
}
提前致谢。
您需要在调用 UIImageWriteToSavedPhotosAlbum
之前拍摄您的 UIImageView
的快照。将以下扩展名添加到 UIView
并调用此函数:
func takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, false, UIScreen.mainScreen().scale);
self.drawViewHierarchyInRect(self.myImageView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
我正在尝试在我的应用程序中开发一个 "Save image" 功能,用户可以在其中将 TableView 中的图像保存在他们的照片库中。我在社区中搜索了很多,但找不到与我的案例相关的任何内容,所以我将对其进行解释:
正如官方指南中所述,UIImageWriteToSavedPhotosAlbum 必须接收一个 UIImage,因此我试图将我的 UIImageView 转换为 UIImage 以实现这一点,但是当一切正常且没有警告或错误时,APP显示错误 "Data unavailable".
我需要做什么才能让它发挥作用?我是否发送了错误的数据?
<UIImage: 0x16db7780>, {400, 345}
这是我的代码:
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error == nil
{
let ac = UIAlertController(title: "Imagen guardada", message: "", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
else
{
let ac = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(ac, animated: true, completion: nil)
}
}
@IBAction func handleGesture(sender: AnyObject) {
self.votarFrame?.hidden = true
let guardarMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
var img:UIImage!
img = self.productoImageView.image
let saveImage = UIAlertAction(title: "Guardar imagen", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
UIImageWriteToSavedPhotosAlbum(img, self, "image:didFinishSavingWithError:contextInfo:", nil)
})
let cancelAction = UIAlertAction(title: "Cancelar", style: UIAlertActionStyle.Cancel, handler: nil)
guardarMenu.addAction(saveImage)
guardarMenu.addAction(cancelAction)
self.presentViewController(guardarMenu, animated: true, completion: nil)
}
提前致谢。
您需要在调用 UIImageWriteToSavedPhotosAlbum
之前拍摄您的 UIImageView
的快照。将以下扩展名添加到 UIView
并调用此函数:
func takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.myImageView.bounds.size, false, UIScreen.mainScreen().scale);
self.drawViewHierarchyInRect(self.myImageView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}