从Iphone中的照片中获取图像

时间:2012-11-28 12:20:30

标签: iphone objective-c ios

我想通过名字从iphone中的图库中获取图像.. 就像我在gallery one.png,two.png,three.png等中有10张照片一样。 我想用two.png。我怎么能得到那个形象。并在我的UIImageView中使用它。 这可能吗?

2 个答案:

答案 0 :(得分:2)

imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)

在这个方法中写

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[ImageViewName setImage:image];
[picker1 dismissViewControllerAnimated:YES completion:nil];

答案 1 :(得分:1)

从图片库中选择图片,您可以使用UIImagePickerController

如果您拥有图片的assetlibrary网址,则可以使用以下代码:

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);    

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [myasset aspectRatioThumbnail];

    UIImage *images;
    if (iref) 
    {
        images = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
    }
    else
    {
        images = [UIImage imageNamed:@"Noimagey.png"];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
            yourImageView.contentMode = UIViewContentModeScaleAspectFit;
            [yourImageView setImage:images];

            });
};

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    UIImage *images = [UIImage imageNamed:@"Nofile.png"];
    dispatch_async(dispatch_get_main_queue(), ^{

        [yourImageView setImage:images];

        });

};    

NSURL *asseturl = [NSURL URLWithString:yourAssetUrl];
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:asseturl resultBlock:resultblock failureBlock:failureblock];

如果您使用资产库来挑选图像,请始终记住:

  1. 这是一个异步过程
  2. 始终将图片绑定到资产块中主线程中的imageView