2014-03-27 59 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。