2016-08-07 11 views
0

몇 가지 예를 살펴본 결과이 사례를 찾아 낼 수 없습니다. 나는 여기서 간단한 것을 놓치고 있다고 느낍니다.PhotosFramework가 앨범에 저작물을 할당합니다.

내 앱에서 특정 앨범 (ABC)의 이미지를 가져 와서이 앱 전용으로 만든 다른 앨범 (XYZ)에 추가하고 싶습니다. 두 앨범 모두 IOS의 기본 사진 앱에서 볼 수 있습니다.

다른 사본을 저장하면 이미지를 앨범 XYZ에 성공적으로 할당 할 수 있습니다. 즉, 이제 카메라 롤로 돌아 가면 같은 이미지의 두 사본을 볼 수 있습니다. 하나는 앨범 ABC에 할당되고 다른 하나는 XYZ에 할당됩니다. 이것은 내가 원하는 것이 아닙니다. 나는 앨범 XYZ에 할당 될 수천 개의 이미지를 가지고 있으며 두 장의 복사본이 공간을 낭비하지 않게하고 싶습니다. 나는 IOS 사진 앱 사용하는 여러 앨범에 이미지를 할당 할 수 있으며 여러 복사본을 생성하지 않는 것을 알고

-(void) saveImage:(UIImage*) imageToSave toCollection:(NSString *) collectionTitle forRowNumber:(long) rowNumber{ 

    NSLog(@"entered %s", __PRETTY_FUNCTION__); 
    __block PHFetchResult *photosAsset; 
    __block PHAssetCollection *collection; 
    __block PHObjectPlaceholder *placeholder; 

     // Find the album 
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 

    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", collectionTitle]; 
       // this is how we get a match for album Title held by 'collectionTitle' 

    collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions].firstObject; 

     // check if album exists 
    if (!collection) 
    { 
     [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 

      NSLog(@" Album did not exist, now creating album: %@",collectionTitle); 
       // Create the album 
      PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionTitle]; 

      placeholder = [createAlbum placeholderForCreatedAssetCollection]; 

     } completionHandler:^(BOOL didItSucceed, NSError *error) { 
      if (didItSucceed) 
      { 
       PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier] options:nil]; 

       collection = collectionFetchResult.firstObject; 
      } 
     }]; 
    } 

     // Save to the album 
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 

     PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave]; 

     placeholder = [assetRequest placeholderForCreatedAsset]; 
     photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil]; 

     PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection assets:photosAsset]; 

     [albumChangeRequest addAssets:@[placeholder]]; 

    } completionHandler:^(BOOL didItSucceed, NSError *error) { 

     if (didItSucceed) 
     {  // if YES 


      NSLog(@" Looks like Image was saved in camera Roll as %@", placeholder.localIdentifier); 
      NSLog(@"placeholder holds %@", placeholder.debugDescription); 

     } 
     else 
     { 
      NSLog(@"%@", error); 
     } 

    }]; 


} 

:

다음은 새 앨범에 이미지의 복사본을 저장 내 코드입니다. 당신이 PHAsset 원본 이미지를 나타내는 경우

PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave]; 

할 수 : 명시 적으로 새로운 이미지 라이브러리에 추가 할 것을 요청하고 있기 때문에 당신은 새로운 이미지를 얻고

답변

3

은 라이브러리에 추가 동일한 애셋을 두 번째 앨범에 추가하도록 요청하십시오. 하지만 공유 한 코드는 UIImage부터 시작하므로 원래 애셋과의 연결이 끊어졌습니다. - 당신은 앨범을 만들 동일한에 추가 할 수

- (void)saveAsset:(PHAsset *)asset toCollectionNamed:(NSString *) collectionTitle { 

    // Find the album 
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", collectionTitle]; 
    PHAssetCollection *collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions].firstObject; 

    // Combine (possible) album creation and adding in one change block 
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
     PHAssetCollectionChangeRequest *albumRequest; 
     if (collection == nil) { 
      // create the album if it doesn't exist 
      albumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionTitle]; 
     } else { 
      // otherwise request to change the existing album 
      albumRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection]; 
     } 
     // add the asset to the album 
     [albumRequest addAssets:@[asset]]; 

    } completionHandler:^(BOOL success, NSError *error) { 
     if (!success) { 
      NSLog(@"error creating or adding to album: %@", error); 
     } 
    }]; 
} 

공지 사항이 두 PHPhotoLibrary performChanges 블록을 필요가 없습니다

곳을 PHAsset이, 당신이 필요할 것 것은 이런 식으로 뭔가를 보이는 것을 한 가정 변경 요청.

+1

완벽한, 감사합니다 !! 이것은 정확히 내가 필요로하는 방식으로 작동합니다. 동일한 공연 변경 요청을 사용하는 보너스가 추가되었습니다. – Pankaj

관련 문제