图像不显示UIImagePickerControllerReferenceURL图像路径

时间:2014-03-27 08:34:08

标签: ios iphone objective-c cocoa-touch

我使用UIImagePickerControllerReferenceURL从照片库中选择了图片,我获得了图片路径并分配给imageView.image仍未显示该图片,下面是我的代码。

- (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];

}

2 个答案:

答案 0 :(得分: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];

 }

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

答案 1 :(得分:0)

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

如上面的代码所述,您可以使用UIImagePickerControllerOriginalImage键从信息字典中获取UIImage。

然后将此UIImage分配给UIImageView。