iOS的相簿建立與圖片保存

作品鏈接:http://www.itdecent.cn/users/1e0f5e6f73f6/top_articles


1.導(dǎo)入第三方框架SVProgressHUD 和 Photos框架


#import<SVProgressHUD.h>

#import<Photos/Photos.h>




2.創(chuàng)建全局變量相簿的名稱

static NSString * PHAssetCollectionTitle = @"卡卡";


3.保存按鈕

- (IBAction)save {

// 判斷授權(quán)狀態(tài)

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusRestricted) {

[SVProgressHUD showErrorWithStatus:@"因?yàn)橄到y(tǒng)原因,無(wú)法訪問(wèn)相冊(cè)"];

} else if (status == PHAuthorizationStatusDenied){

// 用戶拒絕當(dāng)前應(yīng)用訪問(wèn)相冊(cè)(用戶當(dāng)初點(diǎn)擊了"不允許")

NSLog(@"提醒用戶去[設(shè)置-隱私-照片-xxx]打開(kāi)訪問(wèn)開(kāi)關(guān)");

} else if (status == PHAuthorizationStatusAuthorized){

//用戶允許當(dāng)前應(yīng)用訪問(wèn)相冊(cè)? 用戶當(dāng)初點(diǎn)擊了好

[self saveImage];

} else if (status == PHAuthorizationStatusNotDetermined){

// 用戶還沒(méi)有做出選擇

// 彈框請(qǐng)求用戶授權(quán)

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

if (status == PHAuthorizationStatusAuthorized) {

[self saveImage];

}

}];

}

}


4.保存圖片

- (void)saveImage

{

// PHAsset : 一個(gè)資源, 比如一張圖片\一段視頻

// PHAssetCollection : 一個(gè)相簿

// PHAsset的標(biāo)識(shí),利用這個(gè)標(biāo)識(shí)可以找到對(duì)應(yīng)的PHAsset對(duì)象 即圖片對(duì)象

__block NSString *assetLocalIdentifier = nil;

// 如果想對(duì)"相冊(cè)"進(jìn)行修改(增刪改), 那么修改代碼必須放在[PHPhotoLibrary sharedPhotoLibrary]的performChanges方法的block中

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

//1.保存圖片A到【相機(jī)膠卷】中

// 創(chuàng)建圖片的請(qǐng)求

assetLocalIdentifier = [PHAssetCreationRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;

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

if (success == NO) {

[self showError:@"保存圖片失敗"];

return ;

}

// 2.獲得相簿

PHAssetCollection *createdAssetCollection = [self createdAssetCollection];

if (createdAssetCollection == nil) {

[self showError:@"保存圖片失敗"];

return ;

}

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

// 獲得圖片

PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil].lastObject;

// 添加圖片到相簿中的請(qǐng)求

PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];

// 添加圖片到相簿

[request addAssets:@[asset]];

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

if (success == NO) {

[self showError:@"保存圖片失敗"];

} else{

[self showSuccess:@"保存圖片成功"];

}

}];

}];

}


5.獲得相簿

- (PHAssetCollection *)createdAssetCollection{??

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

for (PHAssetCollection *assetCollection in assetCollections) {

if ([assetCollection.localizedTitle isEqualToString:PPHAssetCollectionTitle]) {

return assetCollection;

}

}

// 沒(méi)有找到對(duì)應(yīng)的相簿, 得創(chuàng)建新的相簿

// 錯(cuò)誤信息

NSError *error = nil;

// PHAssetCollection的標(biāo)識(shí),利用這個(gè)標(biāo)識(shí)可以找到對(duì)應(yīng)的PHAssetCollection的對(duì)象(相簿對(duì)象)

__block NSString *assetCollectionLocalIdentifier = nil;

[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{

// 創(chuàng)建相簿的請(qǐng)求

assetCollectionLocalIdentifier = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:PPHAssetCollectionTitle].placeholderForCreatedAssetCollection.localIdentifier;

} error:&error];

if (error) return nil;

// 獲得剛才創(chuàng)建的相簿

PHAssetCollection *assetCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionLocalIdentifier] options:nil].lastObject;

return assetCollection;

}


6.保存成功方法

- (void)showSuccess:(NSString *)text

{

dispatch_async(dispatch_get_main_queue(), ^{

[SVProgressHUD showSuccessWithStatus:text];

});

}


7.保存失敗方法

- (void)showError:(NSString *)text

{

dispatch_async(dispatch_get_main_queue(), ^{

[SVProgressHUD showErrorWithStatus:text];

});

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容