使用照片框架ios 8将图像保存到特定的相册

时间:2015-12-01 14:06:10

标签: objective-c iphone ios8 photo

我刚用这段代码创建了一个新的相册:

 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
     {
         //Checks for App Photo Album and creates it if it doesn't exist
         PHFetchOptions *fetchOptions = [PHFetchOptions new];
         fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title == %@", @"ChameleonSensors"];
         PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:fetchOptions];

         if (fetchResult.count == 0)
         {
             //Create Album
             PHAssetCollectionChangeRequest *albumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"ChameleonSensors"];
         }

     }

      completionHandler:^(BOOL success, NSError *error)
     {
         NSLog(@"Log here...");

         if (!success) {
             NSLog(@"Error creating album: %@", error);
         }else{
             NSLog(@"Perfecto");
         }
     }];

这没关系,但现在我需要在这个相册中保存照片。

我正在使用objective-c和照片框架。

谢谢

1 个答案:

答案 0 :(得分:0)

您应该将资源添加到albumRequest

    //Create Album
    PHAssetCollectionChangeRequest *albumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"ChameleonSensors"];
    PHAssetChangeRequest *createImageRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageNamed:@"test"]];
    [albumRequest addAssets:@[createImageRequest.placeholderForCreatedAsset]];