UIImage不保存到相机胶卷

时间:2015-05-17 03:15:30

标签: ios iphone swift camera core-image

我目前正在为Swift中的iOS编写照片应用。我使用CoreImage Framework在用户选择的UIImageView上生成像素效果。但是,我在保存" pixeled"图像到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的连接   被打断或资产已经死亡

2 个答案:

答案 0 :(得分: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 
}]; 

答案 1 :(得分:1)

我能够使用此代码解决问题:

@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)
}