1

I want to change the title of Photo Album.

So I tried

- (IBAction)reNameTitle:(id)sender {
PHAssetCollection *myCollection = [self.collectionArray objectAtIndex:[sender tag]];

PHFetchResult *results = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

    [results enumerateObjectsUsingBlock:^(PHCollectionList *collectionList, NSUInteger idx, BOOL *stop)
     {
         if ([collectionList.localizedTitle isEqualToString:myCollection.localizedTitle])
         {
             PHCollectionListChangeRequest *collectionChangeRequest = [PHCollectionListChangeRequest changeRequestForCollectionList:collectionList];
             collectionChangeRequest.title = @"newName";
             NSLog(@"collectionChangeRequest - %@", collectionChangeRequest);
             //This log result is ----- "collectionChangeRequest - <PHCollectionListChangeRequest: 0x170268480> title=newName hasAssetChanges=0"
         }
     }];
} completionHandler:^(BOOL success, NSError *error) {
    NSLog(@"Finished editing collection. %@", (success ? @"Success." : error));
    //This log result is ----- "Finished editing collection. Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)""
}];

}

First log is changed title "newName" but second log is "error CODE=-1" and my album title is not changing. What is the problem in my code??

rmaddy
  • 314,917
  • 42
  • 532
  • 579
GioHong
  • 51
  • 4

1 Answers1

1
    -(void)changeAlbumtitle:(PHAssetCollection *)collection withTitle:(NSString *)title {

    if (![collection canPerformEditOperation:PHCollectionEditOperationRename]) {

        NSLog(@"can't PerformEditOperation");
        return;
    }


    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCollectionChangeRequest *changeTitlerequest =[PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
        changeTitlerequest.title = title;


    } completionHandler:^(BOOL success, NSError *error) {
            NSLog(@"Finished editing collection. %@", (success ? @"Successfully." : error));
    }];


}

In Above give function pass the album object (PHAssetCollection ) and title

  • First the function check PHAssetCollection can perform rename editOperation
  • If edit operation is possible then implement PHAssetCollectionChangeRequest and set the new title.
Adarsh G J
  • 2,684
  • 1
  • 24
  • 25