1

我想更改相册的标题。

所以我尝试了

- (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.)""
}];

}

第一个日志的标题更改为“newName”,但第二个日志的标题为“error CODE=-1”,我的专辑标题没有更改。我的代码有什么问题?

4

1 回答 1

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));
    }];


}

在上面给函数传递专辑对象(PHAssetCollection)和标题

  • 首先检查 PHAssetCollection 函数是否可以执行重命名编辑操作
  • 如果可以进行编辑操作,则执行 PHAssetCollectionChangeRequest 并设置新标题。
于 2015-11-25T05:53:25.420 回答