将照片保存到照片库中的文件夹

时间:2014-02-27 00:20:29

标签: ios iphone objective-c cocoa-touch

所以我知道使用以下内容将图像保存到普通相机胶卷:

UIImage *image = imageView.image;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 

但我想要做的是将照片保存到我照片库中的特定命名文件夹中。例如,我用Instagram拍摄的任何照片都会保存到我图书馆的Instagram文件夹中。这也适用于Snapchat。

1 个答案:

答案 0 :(得分:2)

首先,您需要创建一个文件夹:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library addAssetsGroupAlbumWithName:folderName 
                              resultBlock:^(ALAssetsGroup *group) 
{
         NSLog(@"Added folder:%@", folderName);
}
                             failureBlock:^(NSError *error) 
{
         NSLog(@"Error adding folder");
}];

然后,找到文件夹:

__block ALAssetsGroup* folder;

[library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                             usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
{
      if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:folderName]) 
      {
          folder = group;
      }
}
                           failureBlock:^(NSError* error) 
{
    // Error handling.    
}];

并将照片添加到其中。

将图像保存到资源库,并将其放入相册:

[library writeImageToSavedPhotosAlbum:yourImage
                                  metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
                           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];
         }
                      failureBlock:^(NSError* error)
         {
              // Error handling.
         }];
     }
     else 
     {
         // Error handling.
     }
 }];