将图像保存到特殊专辑swift

时间:2014-10-05 21:18:09

标签: image swift ios8

我想在某张相册中添加一张新照片,我试图这样做:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    albumName = "\(data.objectAtIndex(indexPath.row))"

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Choose image source", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

    actionSheet.addAction(UIAlertAction(title: "Take Photo", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
        self.presentViewController(imagePickerController, animated: true, completion: {})

    }))

    actionSheet.addAction(UIAlertAction(title: "Camera Roll", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        self.presentViewController(imagePickerController, animated: true, completion: nil)

    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

    self.presentViewController(actionSheet, animated: true, completion: nil)

}


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let image:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage

    var groupToAddTo: ALAssetsGroup = ALAssetsGroup()

    self.library.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAlbum),
        usingBlock: {
            (group: ALAssetsGroup!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
            if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumName){
                groupToAddTo = group
            }
        },
        failureBlock: {
            (myerror: NSError!) -> Void in
            println("error occurred: \(myerror.localizedDescription)")
    })

    var img: CGImageRef = image.CGImage

    self.library.writeImageToSavedPhotosAlbum(img, metadata: nil, completionBlock: {
        (assetUrl: NSURL!, error: NSError!) -> Void in
        if error.code == 0 {
            println("saved image completed: \(assetUrl)")

            self.library.assetForURL(assetUrl, resultBlock: { (asset: ALAsset!) -> Void in
                groupToAddTo.addAsset(asset)
                return
                }, failureBlock: {
                    (myerror: NSError!) -> Void in
                    println("error occurred: \(myerror.localizedDescription)")
            })
        } else {
            println("saved image failed. \(error.localizedDescription) code \(error.code)")
        }
    } )




}

但是,当我运行此代码时,我收到此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

在这一行:

if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumName){

所以,问题是,如何在swift中将图像保存到某个专辑?

2 个答案:

答案 0 :(得分:1)

我认为albumName是一个可选值。

您应该使用if语句来判断almbumName是否为零。如果是nil,请在iPhone上创建一个新的名称。

答案 1 :(得分:1)

首先:

if group != nil {
   if group.valueForProperty(ALAssetsGroupPropertyName).isEqualToString(self.albumNames[indexPath.row]){
       groupToAddTo = group
   }
}

其次,当error = nil时,你的应用程序会因为&#34而崩溃;如果error.code == 0。&#34;如果错误== nil&#34;代替。