iOS的相簿建立與圖片保存

作品鏈接:http://www.lxweimin.com/users/1e0f5e6f73f6/top_articles


1.導入第三方框架SVProgressHUD 和 Photos框架


#import<SVProgressHUD.h>

#import<Photos/Photos.h>




2.創建全局變量相簿的名稱

static NSString * PHAssetCollectionTitle = @"卡卡";


3.保存按鈕

- (IBAction)save {

// 判斷授權狀態

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusRestricted) {

[SVProgressHUD showErrorWithStatus:@"因為系統原因,無法訪問相冊"];

} else if (status == PHAuthorizationStatusDenied){

// 用戶拒絕當前應用訪問相冊(用戶當初點擊了"不允許")

NSLog(@"提醒用戶去[設置-隱私-照片-xxx]打開訪問開關");

} else if (status == PHAuthorizationStatusAuthorized){

//用戶允許當前應用訪問相冊? 用戶當初點擊了好

[self saveImage];

} else if (status == PHAuthorizationStatusNotDetermined){

// 用戶還沒有做出選擇

// 彈框請求用戶授權

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

if (status == PHAuthorizationStatusAuthorized) {

[self saveImage];

}

}];

}

}


4.保存圖片

- (void)saveImage

{

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

// PHAssetCollection : 一個相簿

// PHAsset的標識,利用這個標識可以找到對應的PHAsset對象 即圖片對象

__block NSString *assetLocalIdentifier = nil;

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

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

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

// 創建圖片的請求

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;

// 添加圖片到相簿中的請求

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;

}

}

// 沒有找到對應的相簿, 得創建新的相簿

// 錯誤信息

NSError *error = nil;

// PHAssetCollection的標識,利用這個標識可以找到對應的PHAssetCollection的對象(相簿對象)

__block NSString *assetCollectionLocalIdentifier = nil;

[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{

// 創建相簿的請求

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

} error:&error];

if (error) return nil;

// 獲得剛才創建的相簿

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

});

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容