2014-03-27 58 views
1

我從照片庫中使用UIImagePickerControllerReferenceURL選擇圖像,我得到圖像路徑和分配給imageView.image仍然不顯示該圖像,波紋管是我的代碼。圖像不顯示形式UIImagePickerControllerReferenceURL圖像路徑

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 

    NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];  
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset) { 

     ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation]; 

     NSString *homeDirectoryPath = NSHomeDirectory(); 
     NSString *imagName = [imageRep filename]; 
     NSString *imagePath = [homeDirectoryPath stringByAppendingString:imagName]; 

     imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)]; 
     imageView.image= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imagePath]]]; 

     UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(100,100,100,50)]; 
     aView.backgroundColor=[UIColor lightGrayColor]; 

     [aView addSubview:imageView]; 
     [categoryDisplayScrollView addSubview:aView]; 
     [self dismissViewControllerAnimated:YES completion:NULL]; 

     }; 

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 
    [assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil]; 

} 

回答

0

您可以使用UIImagePickerControllerOriginalImage獲取圖像。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
     UIImage *imagepicked = [info objectForKey:UIImagePickerControllerOriginalImage]; 

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset) { 

     ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation]; 

     NSString *homeDirectoryPath = NSHomeDirectory(); 
     NSString *imagName = [imageRep filename]; 
     NSString *imagePath = [homeDirectoryPath stringByAppendingString:imagName]; 

     imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)]; 
     imageView.image= imagepicked; 

     UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(100,100,100,50)]; 
     aView.backgroundColor=[UIColor lightGrayColor]; 

     [aView addSubview:imageView]; 
     [categoryDisplayScrollView addSubview:aView]; 
     [self dismissViewControllerAnimated:YES completion:NULL]; 

     }; 

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 
    [assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil]; 

} 

希望它可以幫助你....!

0
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo (NSDictionary *)info 
{ 
    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage]; 
    [picker dismissViewControllerAnimated:YES completion:nil]; 
} 

正如在上面的代碼中提到的,您可以從信息字典中使用UIImagePickerControllerOriginalImage鍵獲取UIImage。

接下來是將此UIImage分配給UIImageView。