2015-05-14 72 views
6

我需要在這裏一點點幫助,我有一個保存到UIImage的相機膠捲沒有的iOS 8問題的方法,該方法如下保存在iOS的照片自定義相冊8

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
    [PHAssetChangeRequest creationRequestForAssetFromImage:image]; 
}completionHandler:^(BOOL success, NSError *error) { 
    if(success){ 
     NSLog(@"worked"); 
    }else{ 
     NSLog(@"Error: %@", error); 

    } 
}]; 

我需要適應的代碼,從而使圖像的,而不是保存的UIImage到相機膠捲,將其保存到一個名爲「通過MyAlbum」

我使用Photos.framework

+0

與論壇網站不同,我們不使用「謝謝」或「任何幫助表示」,或在[so]上簽名。請參閱「[應該'嗨','謝謝',標語和致敬從帖子中刪除?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be -removed-from-posts) –

回答

0

在自定義專輯您的執行更改塊,您指定請求的assetCollection

let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(-YOUR ALBUM NAME-)

+0

哪些執行更改block我應該更改 –

7

在將資源添加到相冊之前,創建一個名爲「MyAlbum」的新相冊。

// Create new album. 
    __block PHObjectPlaceholder *albumPlaceholder; 
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
     PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title]; 
     albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection; 
    } completionHandler:^(BOOL success, NSError *error) { 
     if (success) { 
      PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil]; 
      PHAssetCollection *assetCollection = fetchResult.firstObject; 

      // Add it to the photo library 
      [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
       PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; 

       PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection]; 
       [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]]; 
      } completionHandler:^(BOOL success, NSError *error) { 
       if (!success) { 
        NSLog(@"Error creating asset: %@", error); 
       } 
      }]; 
     } else { 
      NSLog(@"Error creating album: %@", error); 
     } 
    }]; 
+4

我們不應該檢查該集合名稱是否已經存在嗎?或者是由框架處理? –

+1

添加到@AndyIbanez,我可以確認此方法並不檢查明礬是否存在,並且每次以這種方式保存照片時都會創建一個新的相冊。 – Jake

+0

如何在應用程序被殺後獲取相冊? – Priyal

11

你首先需要檢查該專輯爲獲取請求存在,那麼無論是圖像添加到相冊或創建相冊,然後添加圖像。

#import <Photos/Photos.h> 

- (void)addImageToCameraRoll:(UIImage *)image { 
    NSString *albumName = @"MyAlbum"; 

    void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) { 
     [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
      PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; 
      PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection]; 
      [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]]; 

     } completionHandler:^(BOOL success, NSError *error) { 
      if (!success) { 
       NSLog(@"Error creating asset: %@", error); 
      } 
     }]; 
    }; 

    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; 
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", albumName]; 
    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions]; 
    if (fetchResult.count > 0) { 
     saveBlock(fetchResult.firstObject); 
    } else { 
     __block PHObjectPlaceholder *albumPlaceholder; 
     [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
      PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName]; 
      albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection; 

     } completionHandler:^(BOOL success, NSError *error) { 
      if (success) { 
       PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil]; 
       if (fetchResult.count > 0) { 
        saveBlock(fetchResult.firstObject); 
       } 
      } else { 
       NSLog(@"Error creating album: %@", error); 
      } 
     }]; 
    } 
} 
0

檢查相冊是否已經存在。

NSString *localIdentifier; 

    PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; 

    for (PHAssetCollection *assetCollection in assetCollections) { 
     if([[assetCollection localizedTitle] isEqualToString:album] ){ 
      localIdentifier = assetCollection.localIdentifier; 
      break; 
     } 

    } 

    if(localIdentifier){ 
     ///fetch album 
     PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[localIdentifier] options:nil]; 

    }else{ 
     ///creat album here 

    } 
相關問題