2013年5月28日 星期二

iOS_Get_PhotoLibrary_Img_and_URL

期末實在是有夠崩潰QQ,實作簡報軟體小細節也太多惹~囧,記錄一下如何從photoLibrary得到照片和照片的URL
1. 獲得UIImagePickerController點擊的照片:
//實作以下delegate
//此delegate會再點下照片反應
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
2. 獲得照片的方法
//寫在1.中的delegate內
//得到照片
UIImage *selectImage =[info objectForKey:UIImagePickerControllerOriginalImage];
//得到touch照片的URL
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
3. 獲得相簿中的URL後無法直接用NSData獲得,需要藉由AssetsLibrary,並實作以下block才能夠得到該UIImage
//以下實作一block來改變(UIButton *)btn的backgroundImage
//block
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myAsset){
        
        ALAssetRepresentation *rep = [myAsset defaultRepresentation];
        CGImageRef iref = [rep fullScreenImage];
        if(iref){
            [btn setBackgroundImage:[UIImage imageWithCGImage:iref 
                                                        scale:[rep scale] orientation:0] 
                           forState:UIControlStateNormal];
        }
    };
    
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
    };
//using above block to set image
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
    [assetsLibrary assetForURL:url resultBlock:resultBlock failureBlock:failureblock];

沒有留言:

張貼留言