简体   繁体   中英

saving image to custom photo album in iOS7

I'm trying to save a photo taken with the camera to a custom photo album in iOS7. my code looks like this:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block ALAssetsGroup* folder;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                       usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
    if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:@"UrbanAlphabets"])
    {
        folder = group;
    }
}
                     failureBlock:^(NSError* error)
{
    // Error handling.
}];
[library writeImageToSavedPhotosAlbum:(__bridge CGImageRef)(image)
                             metadata:nil
                      completionBlock:^(NSURL* assetURL, NSError* error)
{
    if (error.code == 0)
    {
        // Get the asset
        [library assetForURL:assetURL
                 resultBlock:^(ALAsset *asset)
         {
             // Assign the photo to the album
             [folder addAsset:asset];
             NSLog(@"success");
         }
                failureBlock:^(NSError* error)
         {
             // Error handling.
             NSLog(@"error1");
         }];


    }
    else
    {
        // Error handling.
        NSLog(@"error");
    }
}];

and actually the console logs "success", so I guess everything should be fine, but it doesn't put the photo into the folder... I pretty much copypasted the code from here http://www.ggkf.com/iphone/save-a-photo-to-a-folder-in-photo-library any ideas?

this made it work

//write to photo library
NSString *albumName=@"Urban Alphabets";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block ALAssetsGroup* groupToAddTo;
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                       usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                           if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:albumName]) {
                               groupToAddTo = group;
                           }
                       }
                     failureBlock:^(NSError* error) {
                     }];
CGImageRef img = [croppedImage CGImage];
[library writeImageToSavedPhotosAlbum:img
                             metadata:nil
                      completionBlock:^(NSURL* assetURL, NSError* error) {
                          if (error.code == 0) {

                              // try to get the asset
                              [library assetForURL:assetURL
                                       resultBlock:^(ALAsset *asset) {
                                           // assign the photo to the album
                                           [groupToAddTo addAsset:asset];
                                       }
                                      failureBlock:^(NSError* error) {
                                      }];
                          }
                          else {
                          }
                      }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM