2

I am using following code to save image to my custom folder created in iOS photo gallery:

I get this error: Error Domain=NSCocoaErrorDomain Code=-1 "(null)"

Can anyone help me out on this ?

+(BOOL)saveToAlbum:(NSString*)albumName image:(UIImage*)image{
__block PHFetchResult *photosAsset;
__block PHAssetCollection *collection;
__block PHObjectPlaceholder *placeholder;
__block BOOL retStat = false;
NSData *newImageSize = UIImageJPEGRepresentation(image, 1);

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageWithData:newImageSize]];
    placeholder = [assetRequest placeholderForCreatedAsset];
    photosAsset = [PHAsset fetchAssetsInAssetCollection:[self findAlbumAssetCollection:albumName] options:nil];
    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection
                                                                                                                  assets:photosAsset];
    [albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
    if (success){
        retStat = true;
    }
    else{
        NSLog(@"%@", error);
    }}];
}
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Saurabh Bisht
  • 389
  • 1
  • 13
  • why did you declare the variables as block variables when you are not changing them outside the block ? You are only chnging the BOOL value outside the block. – Teja Nandamuri Oct 08 '15 at 14:38
  • So does that mean that the problem is because of declaring variables as block variable? – Saurabh Bisht Oct 08 '15 at 14:46
  • that couldnt be the problem,but try to see by declaring the variables inside the block and also check if the nsdata is nil and what is your collection? – Teja Nandamuri Oct 08 '15 at 14:49
  • Ok, I am trying to save the image in the ios galary folder using PHPhotoLibrary, do you see any problem with the code, I got the code refference from some link over internet... i have here only two things one is the album name where i have to save and the other is the image which i have to save. Can you give a better answer to this? – Saurabh Bisht Oct 08 '15 at 14:58
  • Nsdata is not nil, it has a value – Saurabh Bisht Oct 08 '15 at 14:59

2 Answers2

3

Answering here so someone can find this in google search results and save themselves the time I wasted: If you get this error creating an asset, check your phone storage. I got this error as a propagation of another silent error because my phone had 0 bytes (yes, 0 bytes) available storage space.

Dennis L
  • 1,713
  • 14
  • 21
1

Thanks Mr.T for providing me a hint(collection was nil at albumChangeRequest)..Here is the correct solution for this.

+(BOOL)saveToAlbum:(NSString*)albumName image:(UIImage*)image{
__block BOOL retStat = false;
NSData *newImageSize = UIImageJPEGRepresentation(image, 1);
UIImage *imageNew = [UIImage imageWithData:newImageSize];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
     PHFetchResult *photosAsset;
     PHAssetCollection *collection = [self findAlbumAssetCollection:albumName];
     PHObjectPlaceholder *placeholder;
    placeholder = [[PHAssetChangeRequest creationRequestForAssetFromImage:imageNew] placeholderForCreatedAsset];
    photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
    PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection assets:photosAsset];                                                                       
    [albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
    if (success){
        retStat = true;
    }
    else{
        NSLog(@"%@", error);
    }}];
return  retStat;}


+(PHAssetCollection*)findAlbumAssetCollection:(NSString*)albumName{
PHAssetCollection *collection;
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", albumName];
collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                      subtype:PHAssetCollectionSubtypeAny
                                                      options:fetchOptions].firstObject;
return collection;

}

Saurabh Bisht
  • 389
  • 1
  • 13