圖片的讀取

通常的實現是使用UIImagePickerController控件,而後實現UIImagePickerControllerDelegate裏面的兩個回調方法。 git

- (void)button2Pressed:(id)sender github

{ UIImagePickerController* vc = [[UIImagePickerController alloc] init]; vc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; vc.delegate = self; [self presentViewController:vc animated:YES completion:nil]; [vc release]; } #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // 圖片在這裏 UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self dismissViewControllerAnimated:YES completion:nil]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissViewControllerAnimated:YES completion:nil]; }

使用新的方法,首先作如下兩點: 1.引入頭文件:#import (尖括號)AssetsLibrary/AssetsLibrary.h(尖括號) 2.在工程中引入庫:AssetsLibrary.framework 具體代碼以下: 異步

// 打印信息,僅作演示
- (void)printALAssetInfo:(ALAsset*)asset
{
    //取圖片的url
    NSString *photoURL=[NSString stringWithFormat:@"%@",asset.defaultRepresentation.url];
    NSLog(@"photoURL:%@", photoURL);
    // 取圖片
    UIImage* photo = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
    NSLog(@"PHOTO:%@", photo);
    NSLog(@"photoSize:%@", NSStringFromCGSize(photo.size));
    // 取圖片縮圖圖
    UIImage* photoThumbnail = [UIImage imageWithCGImage:asset.thumbnail];
    NSLog(@"PHOTO2:%@", photoThumbnail);
    NSLog(@"photoSize2:%@", NSStringFromCGSize(photoThumbnail.size));
}

-(void)loadImageFromPhotoLibrary
{
    // 爲了防止界面卡住,能夠異步執行
    dispatch_async(dispatch_get_main_queue(), ^{
        // 獲取相冊每一個組裏的具體照片
        ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){
            if (result!=nil) {
                // 檢查是不是照片,還多是視頻或其它的
                // 因此這裏咱們還能類舉出枚舉視頻的方法。。。
                if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                    [self printALAssetInfo:result];
                }
            }
        };
        //獲取相冊的組
        ALAssetsLibraryGroupsEnumerationResultsBlock groupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){
            if (group!=nil) {
                NSString *groupInfo=[NSString stringWithFormat:@"%@",group];
                NSLog(@"GROUP INFO:%@",groupInfo);

                [group enumerateAssetsUsingBlock:groupEnumerAtion];
            }
        };

        ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error){
            // 相冊訪問失敗的回調,能夠打印一下失敗緣由
            NSLog(@"相冊訪問失敗,ERROR:%@", [error localizedDescription]);
        };

        ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
        [library enumerateGroupsWithTypes:ALAssetsGroupAll
                               usingBlock:groupsEnumeration
                             failureBlock:failureblock];
        [library release];     
    }); 

}

// 同上面的原理,咱們再作一個根據URL取圖片及縮略圖的方法
- (void)loadImageForURL:(NSURL*)photoUrl
{
    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
    [assetLibrary assetForURL:photoUrl
                  resultBlock:^(ALAsset *asset)
     {
         [self printALAssetInfo:asset];
    }
                 failureBlock:^(NSError *error)
     {
        NSLog(@"error=%@",error);
    }];
}
原文鏈接:http://ilovedev.github.io/blog/2013/11/03/bu-shi-yong-uiimagepickercontrollershi-xian-du-qu-xiang-ce-tu-pian-de-fang-fa/
相關文章
相關標籤/搜索