解決iOS8.0以上系統下無法通過代碼建立被刪除過的同名相簿 ( iOS 8 Photos framework: Create a albums with iOS8 )

來源:互聯網
上載者:User

標籤:

問題描述:

在蘋果的 iOS8.0以上, 當你建立完一個相簿, 例如名為"Rd", 然後在相簿中手動刪除了這個"Rd"相簿, 再次通過代碼

#import <AssetsLibrary/ALAssetsLibrary.h>
 ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];                                                                                                                                                                                                           [assetsLibrary addAssetsGroupAlbumWithName:@"Rd" resultBlock:^(ALAssetsGroup *group){  [groups addObject:group];}
failureBlock:nil];

以上代碼在 iOS 8.0+ 從未建立過 "Rd" 這個相簿的 iPhone 或者 iPad 上面可以成功建立相簿"Rd" ;

在 iOS 8.0 以下是無論什麼情況下都可以建立;

但是在iOS 8.0+ 上如果是已經建立過 "Rd" 這個相簿, 然後 "Rd" 相簿 被手動刪除後的 iPhone 或者 iPad 上面, 代碼執行後 group 是 nil ;

 

問題分析:

個人感覺是跟 iOS 8.0 之後, 相簿新增了 Recently Deleted (最近刪除) 這個功能, 導致我們在相簿裡邊手動刪除 "Rd"相簿 是並沒有完全刪除, 導致建立相簿雖然成功, 但卻為 nil ,因為建立的相簿還是處於 Recently Deleted 的狀態;

 

解決方案:

既然發現無法建立後, 查閱官方文檔, 發現這麼一段話

(在 iOS 8.0 後, 使用the Photos framework 代替 the Assets Library framework , The Photos framework 提供更特色和更好的表現 在使用 photo library 工作的時候)

蘋果官方Photo Framework例子:

https://developer.apple.com/devcenter/download.action?path=/wwdc_2014/wwdc_2014_sample_code/exampleappusingphotosframework.zip

仔細挖掘下然後就能解決啦~

iOS 8.0+ 使用Photos framework 建立相簿代碼

#import <Photos/Photos.h
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{  [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"Rd"];} completionHandler:^(BOOL success, NSError *error) {  if (!success) {    NSLog(@"Error creating album: %@", error);  }}];

 

解決iOS8.0以上系統下無法通過代碼建立被刪除過的同名相簿 ( iOS 8 Photos framework: Create a albums with iOS8 )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.