Garch
Garch

Reputation: 31

Saving an image to special album swift

I want to add a new photo to certain album, I'm trying to do it like that:

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




}

However, when I run this code, I get this error:

fatal error: unexpectedly found nil while unwrapping an Optional value

in this line:

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

So, the question is that, how to save an image to certain album in swift?

Upvotes: 1

Views: 633

Answers (2)

Nick Barr
Nick Barr

Reputation: 574

First:

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

Second, when error = nil your app will crash because of "if error.code == 0." Do "if error == nil" instead.

Upvotes: 1

PeiweiChen
PeiweiChen

Reputation: 413

I am betting albumName is an optional value.

You should put an if statement to judge if almbumName is nil. When it's nil, create a newalbum name on your iPhone.

Upvotes: 1

Related Questions