UIImage 不保存到相机胶卷
Posted
技术标签:
【中文标题】UIImage 不保存到相机胶卷【英文标题】:UIImage not saving to Camera Roll 【发布时间】:2015-05-17 03:15:30 【问题描述】:我目前正在用 Swift 为 ios 编写一个照片应用程序。我正在使用 CoreImage 框架在用户选择的 UIImageView
上生成像素效果。但是,我无法将“像素化”图像保存到 iPhone 的相机胶卷中。通常我使用
UIImageWriteToSavedPhotosAlbum(pixeledImage,nil,nil,nil)
但它没有保存UIImage
。我已授予该应用程序对设备上照片库的完全访问权限。如果有人可以帮助我解决这个问题,那将会很有帮助。我的图片选择器:
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
var imagetobepassed: UIImage!
override func viewDidLoad()
super.viewDidLoad()
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
@IBAction func Cameratapped(sender: AnyObject)
var camera = UIImagePickerController()
dispatch_async(dispatch_get_main_queue())
camera.delegate = self
camera.sourceType = UIImagePickerControllerSourceType.Camera
camera.allowsEditing = false
self.presentViewController(camera, animated: true, completion: nil)
@IBAction func photolib(sender: AnyObject)
var photo = UIImagePickerController()
dispatch_async(dispatch_get_main_queue())
photo.delegate = self
photo.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
photo.allowsEditing = false
self.presentViewController(photo, animated: true, completion: nil)
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!)
imagetobepassed = image
self.dismissViewControllerAnimated(true, completion: nil)
@IBAction func editorPressed(sender: AnyObject)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "image"
var editorview = segue.destinationViewController as! EditorViewController
editorview.imagerecived = imagetobepassed
还有我的编辑:
import UIKit
class EditorViewController: UIViewController
var imagerecived:UIImage!
var pixeledImage:UIImage!
var savedImage:UIImage!
@IBOutlet var imageview: UIImageView!
override func viewDidLoad()
super.viewDidLoad()
pixel()
// this function will prduce the pixel effect
func pixel()
var regularImage = CIImage(image: imagerecived)
var filter = CIFilter(name: "CIPixellate")
filter.setDefaults()
filter.setValue(regularImage, forKey: kCIInputImageKey)
var output = filter.outputImage
pixeledImage = UIImage(CIImage: output)
imageview.image = pixeledImage
@IBAction func SaveTapped(sender: AnyObject)
println(pixeledImage)
UIImageWriteToSavedPhotosAlbum(pixeledImage,nil,nil,nil) // not saving image
我怎样才能做到这一点?我确实看到了这个错误:
2015-05-16 23:40:05.416 Pixelate2[21579:3341726] 与 assetsd 的连接 被打断或资产死亡
【问题讨论】:
你遇到了什么错误? @Vizllx 2015-05-16 23:40:05.416 Pixelate2[21579:3341726] 与 assetsd 的连接被中断或 assetsd 死了 这意味着您收到了内存警告! 【参考方案1】:先用这一行保存图片:-
UIImageWriteToSavedPhotosAlbum(pixeledImage, self, "image:didFinishSavingWithError:contextInfo:", nil)
现在实现这个方法,来捕捉你得到的错误:-
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>)
if error == nil
else
//log the error out here ,if any
如果您收到内存警告,还有另一种解决方法!
ALAssetsLibrary* lib = [[ALAssetsLibrary alloc] init];
[lib writeImageDataToSavedPhotosAlbum:imageData metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error)
// do whatever in the completion handler
];
【讨论】:
我尝试了你的两个答案,但我仍然得到相同的结果。你还有什么想法吗【参考方案2】:我能够使用此代码解决问题:
@IBAction func SaveTapped(sender: AnyObject)
let softwareContext = CIContext(options: [kCIContextUseSoftwareRenderer:true])
let cgimg = softwareContext.createCGImage(savedImage,
fromRect: savedImage.extent())
let libary = ALAssetsLibrary()
libary.writeImageToSavedPhotosAlbum(cgimg, metadata: savedImage.properties(),
completionBlock: nil)
【讨论】:
我实际上以同样的方式解决了一个类似的问题。使用 CoreImage 创建 UIImage 后,我有一个可能尚未完全烘焙的 UIImage。例如,它的cgImage
属性为零。我使用createCGImage()
创建了一个中间图像,然后它就可以工作了。以上是关于UIImage 不保存到相机胶卷的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 UIActivityViewController 将 UIImage 共享到相机胶卷