2014-10-05 32 views
1

我要添加新的照片对某些专辑的形象,我想那样做:保存为特别专辑迅速

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){ 

所以,问题是,如何将图像保存到特定的图册中迅速?

回答

1

我在下注albumName是一个可选值。

您应该输入if语句来判断almbumName是否为nil。当它是nil时,在iPhone上创建一个新的相册名称。

1

第一:

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

其次,当误差=零您的应用程序会崩溃,因为 “如果error.code == 0”。做“如果错误==零”,而不是。