我正在尝试在我的应用程序中开发“保存图像”功能,用户可以在其中将TableView中的图像保存在其照片库中。我在社区中进行了很多搜索,但找不到与我的案件有关的任何内容,因此我将对其进行解释:
如官方指南中所述,UIImageWriteToSavedPhotosAlbum必须接收UIImage,因此我试图将UIImageView转换为UIImage以实现此目的,但是当一切正常而没有警告或错误时,APP显示错误提示“数据不可用”。
我需要做什么才能使其正常工作?我发送的数据错误吗?
<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)
}
提前致谢。
最佳答案
您需要在调用UIImageView
之前对UIImageWriteToSavedPhotosAlbum
进行快照。将以下扩展名添加到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;
}