亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS中讀取照片庫及保存圖片或視頻到照片庫的要點解析

 更新時間:2016年06月07日 09:22:32   作者:劉偉  
iOS中保存到本地的圖片視頻都會被匯總到系統(tǒng)的PhotoLibrary中,這里我們就來看一下iOS中讀取照片庫及保存圖片或視頻到照片庫的要點解析

讀取照片庫PhotoLibrary
iOS中如果我們只有一次讀取一張圖片或者一個視頻(或拍一張照片/視頻)的需求,那么我們用 UIImagePickerController 就可以搞定。但是很多時候我們需要一次性從PhotoLibrary讀取多個照片或者視頻,這時候我們就需要另辟蹊徑了,好在apple為我們提供了相應(yīng)的接口。
在開始coding之前我們想要認識幾個類:
ALAssetsLibrary:代表整個PhotoLibrary,我們可以生成一個它的實例對象,這個實例對象就相當于是照片庫的句柄。
ALAssetsGroup:照片庫的分組,我們可以通過ALAssetsLibrary的實例獲取所有的分組的句柄。
ALAsset:一個ALAsset的實例代表一個資產(chǎn),也就是一個photo或者video,我們可以通過他的實例獲取對應(yīng)的subnail或者原圖等等。
還需要了解的一個東東就是blocks,apple在iOS 4.0以后大量出現(xiàn)了這玩意兒,有使用越來越廣的意思,不過這玩意兒確實好用。關(guān)于這玩意兒的內(nèi)容我在這里不多講,關(guān)注我的博客我會細講。
對于本文的需求,我們讀取group和每個asset都是異步的,但是我們現(xiàn)在用blocks我們可以在一個函數(shù)里面搞定。所以blocks確實很方便。
下面直接看代碼吧:

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

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];//生成整個photolibrary句柄的實例   
NSMutableArray *mediaArray = [[NSMutableArray alloc]init];//存放media的數(shù)組   
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {//獲取所有g(shù)roup   
        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {//從group里面   
            NSString* assetType = [result valueForProperty:ALAssetPropertyType];   
            if ([assetType isEqualToString:ALAssetTypePhoto]) {   
                NSLog(@"Photo");   
            }else if([assetType isEqualToString:ALAssetTypeVideo]){   
                NSLog(@"Video");   
            }else if([assetType isEqualToString:ALAssetTypeUnknown]){   
                NSLog(@"Unknow AssetType");   
            }   
               
            NSDictionary *assetUrls = [result valueForProperty:ALAssetPropertyURLs];   
            NSUInteger assetCounter = 0;   
            for (NSString *assetURLKey in assetUrls) {   
                NSLog(@"Asset URL %lu = %@",(unsigned long)assetCounter,[assetUrls objectForKey:assetURLKey]);   
            }   
               
            NSLog(@"Representation Size = %lld",[[result defaultRepresentation]size]);   
        }];   
    } failureBlock:^(NSError *error) {   
        NSLog(@"Enumerate the asset groups failed.");   
    }];  

保存圖片或視頻到PhotoLibrary
時文件然后,然后通過臨時文件的路徑去轉(zhuǎn)存到photo library。
我們直接來看相應(yīng)的API:

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

// These methods can be used to add photos or videos to the saved photos album. 
 
// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation. 
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock; 
 
// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image 
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1); 
 
// If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten 
- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1); 
 
- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock; 

前三個都是存圖片的,通過參數(shù)我們可以發(fā)現(xiàn),第一個使用了我們傳進去的方向,第二個可以通過傳入image的metadata保留image的metadata,前兩個都是把圖片轉(zhuǎn)成 CGImageRef 再保存,第三個是傳入NSData所以可以完整保留image的信息,同時也有metadata傳進去,如果image自帶的信息與metadata沖突那metadata會覆蓋圖片本身所帶的metadata。
最后一個是存儲視頻的API,可以看到參數(shù)是一個NSURL,這個只要穿一個本地臨時文件的file URL 就好了。
存儲圖片根據(jù)你的需求選擇適當?shù)腁PI,比如我們獲取到的是UIImage的實例,那么我們用第一個或者第二個比較方便,如果我們從本地臨時文件讀取image的數(shù)據(jù)那么我們直接用第三個就比較方便。
下面來一段簡單的代碼:
復(fù)制代碼 代碼如下:

- (void)saveImage:(UIImage*)image{ 
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init]; 
    [assetsLibrary writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) { 
        if (error) { 
            NSLog(@"Save image fail:%@",error); 
        }else{ 
            NSLog(@"Save image succeed."); 
        } 
    }]; 


保存video就麻煩點了,你需要先把video寫入本地文件然后,獲取到本地臨時文件的路徑,然后調(diào)用上面的第四個API寫入photo library。
關(guān)于寫入臨時文件,我之前寫過一篇關(guān)于文件讀寫的文章,可以去看看。
我這里奉上一個把工程資源庫的video寫入photo library的demo,這樣你就可以把video導(dǎo)入模擬器了,方便有些時候測試。
主要代碼如下:
復(fù)制代碼 代碼如下:

- (void)save:(NSString*)urlString{ 
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:urlString] 
                                completionBlock:^(NSURL *assetURL, NSError *error) { 
                                    if (error) { 
                                        NSLog(@"Save video fail:%@",error); 
                                    } else { 
                                        NSLog(@"Save video succeed."); 
                                    } 
                                }]; 

相關(guān)文章

最新評論