iOS保存App中的照片到系統(tǒng)相冊或自建相冊的方法
保存照片到系統(tǒng)相冊
保存照片到系統(tǒng)相冊這個功能很多社交類的APP都有的,今天我們簡單講解一下,如何將圖片保存到系統(tǒng)相冊(Photo Album)。
1.創(chuàng)建UIImageView
創(chuàng)建UIImageView是為了將照片展示出來,我們是要把UIImage保存到系統(tǒng)相冊(Photo Album):
#define SCREEN [UIScreen mainScreen].bounds.size
self.image = [UIImage imageNamed:@"iOSDevTip"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN.width - 300) / 2, 70, 300, 150)];
imageView.image = self.image;
[self.view addSubview:imageView];
2.創(chuàng)建UIButton
創(chuàng)建UIButton并綁定actionClick:事件:
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake( 100, 300, SCREEN.width - 200, 40);
[button addTarget:self action:@selector(actionClick:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
[button setTitle:@"SavePhoto" forState:UIControlStateNormal];
[self.view addSubview:button];
- (void)actionClick:(UIButton *)button
{
}
3.保存照片到系統(tǒng)相冊(Photo Album)
在actionClick:方法里調(diào)用:
UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
這個時候,我們想知道保存是否成功,所以需要制定回調(diào)方法
// 指定回調(diào)方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if(!error){
NSLog(@"save success");
}else{
NSLog(@"save failed");
}
}
在這個方法里,我們就知道照片是否保存成功。
保存照片到自己創(chuàng)建的相簿
接下來,我們來詳細(xì)講解一下關(guān)于系統(tǒng)相冊權(quán)限獲取、保存照片、創(chuàng)建自己的相簿等等功能。
1.創(chuàng)建自己的相簿
這也是一種比較創(chuàng)建的作法,創(chuàng)建自己的相簿,然后把照片或者視頻保存到自己的相簿中。相關(guān)代碼如下:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
//創(chuàng)建相簿成功
} failureBlock:^(NSError *error) {
//失敗
}];
2.保存照片
這個方法也是將照片保存到系統(tǒng)相簿里面,不是保存到自己創(chuàng)建的相簿里面。代碼如下:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *asSetUrl,NSError *error){
if (error) {
//失敗
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存儲成功"
message:nil
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil, nil];
[alert show];
}
}];
3.獲取權(quán)限
在保存照片之前,如果用戶關(guān)閉相冊權(quán)限,這個時候是保存失敗的。如果你不做任何處理,用戶是不會知道自己保存失敗了。所以,我們可以在保存照片之前,做出相應(yīng)的提示。如何獲取這個權(quán)限呢?一般有兩種方法:
(1)創(chuàng)建相簿失敗
(2)保存照片失敗
在上面兩個方法創(chuàng)建自己的相簿和保存照片的失敗結(jié)果里,我們可以彈出獲取照片權(quán)限失敗的提示。我們拿第一個創(chuàng)建相簿失敗來舉例:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {
//創(chuàng)建相簿成功
} failureBlock:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存儲失敗"
message:@"請打開 設(shè)置-隱私-照片 來進行設(shè)置"
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil, nil];
[alert show];
}];
在保存照片失敗的結(jié)果里,我們也可以彈出相應(yīng)的提示,讓用戶打開應(yīng)用程序的相冊權(quán)限。
4.保存照片到自己的相簿
下面這段代碼是在大谷歌里面找到的,親自測試過是可以用的,整理如下:
#pragma mark - 創(chuàng)建相冊
- (void)createAlbum
{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
NSMutableArray *groups=[[NSMutableArray alloc]init];
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[groups addObject:group];
}
else
{
BOOL haveHDRGroup = NO;
for (ALAssetsGroup *gp in groups)
{
NSString *name =[gp valueForProperty:ALAssetsGroupPropertyName];
if ([name isEqualToString:@"iOSDevTip"])
{
haveHDRGroup = YES;
}
}
if (!haveHDRGroup)
{
//do add a group named "XXXX"
[assetsLibrary addAssetsGroupAlbumWithName:@"iOSDevTip"
resultBlock:^(ALAssetsGroup *group)
{
[groups addObject:group];
}
failureBlock:nil];
haveHDRGroup = YES;
}
}
};
//創(chuàng)建相簿
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil];
[self saveToAlbumWithMetadata:nil imageData:UIImagePNGRepresentation(self.image) customAlbumName:@"iOSDevTip" completionBlock:^
{
//這里可以創(chuàng)建添加成功的方法
}
failureBlock:^(NSError *error)
{
//處理添加失敗的方法顯示alert讓它回到主線程執(zhí)行,不然那個框框死活不肯彈出來
dispatch_async(dispatch_get_main_queue(), ^{
//添加失敗一般是由用戶不允許應(yīng)用訪問相冊造成的,這邊可以取出這種情況加以判斷一下
if([error.localizedDescription rangeOfString:@"User denied access"].location != NSNotFound ||[error.localizedDescription rangeOfString:@"用戶拒絕訪問"].location!=NSNotFound){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.localizedFailureReason delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles: nil];
[alert show];
}
});
}];
}
- (void)saveToAlbumWithMetadata:(NSDictionary *)metadata
imageData:(NSData *)imageData
customAlbumName:(NSString *)customAlbumName
completionBlock:(void (^)(void))completionBlock
failureBlock:(void (^)(NSError *error))failureBlock
{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
__weak ALAssetsLibrary *weakSelf = assetsLibrary;
void (^AddAsset)(ALAssetsLibrary *, NSURL *) = ^(ALAssetsLibrary *assetsLibrary, NSURL *assetURL) {
[assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customAlbumName]) {
[group addAsset:asset];
if (completionBlock) {
completionBlock();
}
}
} failureBlock:^(NSError *error) {
if (failureBlock) {
failureBlock(error);
}
}];
} failureBlock:^(NSError *error) {
if (failureBlock) {
failureBlock(error);
}
}];
};
[assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
if (customAlbumName) {
[assetsLibrary addAssetsGroupAlbumWithName:customAlbumName resultBlock:^(ALAssetsGroup *group) {
if (group) {
[weakSelf assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
if (completionBlock) {
completionBlock();
}
} failureBlock:^(NSError *error) {
if (failureBlock) {
failureBlock(error);
}
}];
} else {
AddAsset(weakSelf, assetURL);
}
} failureBlock:^(NSError *error) {
AddAsset(weakSelf, assetURL);
}];
} else {
if (completionBlock) {
completionBlock();
}
}
}];
}
- 總結(jié)iOS App開發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式
- iOS App初次啟動時的用戶引導(dǎo)頁制作實例分享
- iOS App中調(diào)用相冊中圖片及獲取最近的一張圖片的方法
- 詳解在iOS App中自定義和隱藏狀態(tài)欄的方法
- safari調(diào)試iOS app web頁面的步驟
- iOS App開發(fā)中使cell高度自適應(yīng)的黑魔法詳解
- 詳解iOS App開發(fā)中Cookie的管理方法
- IOS App圖標(biāo)和啟動畫面尺寸詳細(xì)介紹
- iOS開發(fā)中使app獲取本機通訊錄的實現(xiàn)代碼實例
- iOS開發(fā)教程之APP內(nèi)部切換語言的實現(xiàn)方法
相關(guān)文章
Swift實現(xiàn)iOS應(yīng)用中短信驗證碼倒計時功能的實例分享
這篇文章主要介紹了Swift實現(xiàn)iOS應(yīng)用中短信驗證碼倒計時功能的實例分享,開啟和關(guān)閉倒計時功能的步驟實現(xiàn)比較關(guān)鍵,需要的朋友可以參考下2016-04-04如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵詳解
xcode是蘋果公司向開發(fā)人員提供的集成開發(fā)環(huán)境,開發(fā)者們經(jīng)常會使用到,下面這篇文章主要給大家介紹了關(guān)于如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵的相關(guān)資料,需要的朋友可以參考下。2018-04-04