欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS保存App中的照片到系統(tǒng)相冊(cè)或自建相冊(cè)的方法

 更新時(shí)間:2016年04月12日 09:06:30   作者:李剛  
這篇文章主要介紹了iOS保存App中的照片到系統(tǒng)相冊(cè)或自建相冊(cè)的方法,示例代碼為傳統(tǒng)的Objective-C語言寫成,需要的朋友可以參考下

保存照片到系統(tǒng)相冊(cè)
保存照片到系統(tǒng)相冊(cè)這個(gè)功能很多社交類的APP都有的,今天我們簡單講解一下,如何將圖片保存到系統(tǒng)相冊(cè)(Photo Album)。

1.創(chuàng)建UIImageView

創(chuàng)建UIImageView是為了將照片展示出來,我們是要把UIImage保存到系統(tǒng)相冊(cè)(Photo Album):

復(fù)制代碼 代碼如下:

#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:事件:

復(fù)制代碼 代碼如下:

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)相冊(cè)(Photo Album)

在actionClick:方法里調(diào)用:

復(fù)制代碼 代碼如下:

UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);

這個(gè)時(shí)候,我們想知道保存是否成功,所以需要制定回調(diào)方法
復(fù)制代碼 代碼如下:

// 指定回調(diào)方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(!error){
        NSLog(@"save success");
    }else{
        NSLog(@"save failed");
    }
}

在這個(gè)方法里,我們就知道照片是否保存成功。

保存照片到自己創(chuàng)建的相簿

接下來,我們來詳細(xì)講解一下關(guān)于系統(tǒng)相冊(cè)權(quán)限獲取、保存照片、創(chuàng)建自己的相簿等等功能。

1.創(chuàng)建自己的相簿

這也是一種比較創(chuàng)建的作法,創(chuàng)建自己的相簿,然后把照片或者視頻保存到自己的相簿中。相關(guān)代碼如下:

復(fù)制代碼 代碼如下:

  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group) {

    //創(chuàng)建相簿成功

} failureBlock:^(NSError *error) {
    //失敗
}];


2.保存照片

這個(gè)方法也是將照片保存到系統(tǒng)相簿里面,不是保存到自己創(chuàng)建的相簿里面。代碼如下:

復(fù)制代碼 代碼如下:

 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:@"存儲(chǔ)成功"
                                                       message:nil
                                                      delegate:nil
                                             cancelButtonTitle:@"確定"
                                             otherButtonTitles:nil, nil];
        [alert show];

    }
}];


3.獲取權(quán)限
在保存照片之前,如果用戶關(guān)閉相冊(cè)權(quán)限,這個(gè)時(shí)候是保存失敗的。如果你不做任何處理,用戶是不會(huì)知道自己保存失敗了。所以,我們可以在保存照片之前,做出相應(yīng)的提示。如何獲取這個(gè)權(quán)限呢?一般有兩種方法:

(1)創(chuàng)建相簿失敗

(2)保存照片失敗

在上面兩個(gè)方法創(chuàng)建自己的相簿和保存照片的失敗結(jié)果里,我們可以彈出獲取照片權(quán)限失敗的提示。我們拿第一個(gè)創(chuàng)建相簿失敗來舉例:

復(fù)制代碼 代碼如下:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"test" resultBlock:^(ALAssetsGroup *group)    {

      //創(chuàng)建相簿成功

} failureBlock:^(NSError *error) {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"存儲(chǔ)失敗"
                                                       message:@"請(qǐng)打開 設(shè)置-隱私-照片 來進(jìn)行設(shè)置"
                                                      delegate:nil
                                             cancelButtonTitle:@"確定"
                                             otherButtonTitles:nil, nil];
    [alert show];
}];


在保存照片失敗的結(jié)果里,我們也可以彈出相應(yīng)的提示,讓用戶打開應(yīng)用程序的相冊(cè)權(quán)限。

4.保存照片到自己的相簿

下面這段代碼是在大谷歌里面找到的,親自測(cè)試過是可以用的,整理如下:

復(fù)制代碼 代碼如下:

#pragma mark - 創(chuàng)建相冊(cè)
- (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í)行,不然那個(gè)框框死活不肯彈出來
         dispatch_async(dispatch_get_main_queue(), ^{

             //添加失敗一般是由用戶不允許應(yīng)用訪問相冊(cè)造成的,這邊可以取出這種情況加以判斷一下
             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();
            }
        }
    }];
}

相關(guān)文章

  • iOS實(shí)現(xiàn)圓角箭頭矩形的提示框

    iOS實(shí)現(xiàn)圓角箭頭矩形的提示框

    不知道大家發(fā)現(xiàn)了沒,在現(xiàn)在的很多App中常使用圓角箭頭矩形, 如微博分組提示框, 地圖坐標(biāo)顯示點(diǎn)等。iPad 中有 UIPopoverController 類供開發(fā)使用, iPhone中就需要開發(fā)人員定制了。那么下面這篇文中就來聊聊定制圓角箭頭矩形提示框,有需要的朋友們可以參考借鑒。
    2016-11-11
  • IOS 簡單的本地json格式文件解析的實(shí)例詳解

    IOS 簡單的本地json格式文件解析的實(shí)例詳解

    這篇文章主要介紹了IOS 簡單的本地json格式文件解析的實(shí)例詳解的相關(guān)資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • iOS中NSPredicate謂詞的使用

    iOS中NSPredicate謂詞的使用

    這篇文章主要給大家介紹了關(guān)于iOS中NSPredicate謂詞的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • iOS關(guān)聯(lián)對(duì)象示例詳解

    iOS關(guān)聯(lián)對(duì)象示例詳解

    這篇文章主要給大家介紹了關(guān)于iOS關(guān)聯(lián)對(duì)象的相關(guān)資料,文中通過示例代碼結(jié)束的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • iOS動(dòng)畫案例(1) 類似于qq賬號(hào)信息里的一個(gè)動(dòng)畫效果

    iOS動(dòng)畫案例(1) 類似于qq賬號(hào)信息里的一個(gè)動(dòng)畫效果

    做一個(gè)類似于qq賬號(hào)信息里的一個(gè)動(dòng)畫,感覺挺有意思,下面給大家分享iOS動(dòng)畫案例(1) 類似于qq賬號(hào)信息里的一個(gè)動(dòng)畫效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-01-01
  • Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享

    Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享

    這篇文章主要介紹了Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享,開啟和關(guān)閉倒計(jì)時(shí)功能的步驟實(shí)現(xiàn)比較關(guān)鍵,需要的朋友可以參考下
    2016-04-04
  • 如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵詳解

    如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵詳解

    xcode是蘋果公司向開發(fā)人員提供的集成開發(fā)環(huán)境,開發(fā)者們經(jīng)常會(huì)使用到,下面這篇文章主要給大家介紹了關(guān)于如何為Xcode添加刪除整行、復(fù)制整行及在下方新建一行快捷鍵的相關(guān)資料,需要的朋友可以參考下。
    2018-04-04
  • IOS 城市定位詳解及簡單實(shí)例

    IOS 城市定位詳解及簡單實(shí)例

    這篇文章主要介紹了IOS 城市定位詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • IOS 仿時(shí)光網(wǎng)選票UI實(shí)例代碼

    IOS 仿時(shí)光網(wǎng)選票UI實(shí)例代碼

    這篇文章主要介紹了IOS 仿時(shí)光網(wǎng)選票UI實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • iOS實(shí)現(xiàn)3D卡片式輪播效果

    iOS實(shí)現(xiàn)3D卡片式輪播效果

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)3D卡片式輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02

最新評(píng)論