繁体   English   中英

显示从iPhone中的ALAsset检索到的URL中的图像

[英]display image from URL retrieved from ALAsset in iPhone

我正在使用ALAsset Framework访问设备照片库中的文件。

到目前为止,我可以访问缩略图并显示它。
我想在图像视图中显示实际图像,但我无法弄清楚如何执行此操作。

我尝试使用ALAsset对象中的URL字段但未成功。

谁知道如何做到这一点?

这是我能够访问缩略图并将其放在表格单元格中的一些代码:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {


  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  }
  //here 'asset' represents the ALAsset object


  asset = [assets objectAtIndex:indexPath.row];
       //i am accessing the thumbnail here
  [cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
  [cell.textLabel setText:[NSString stringWithFormat:@"Photo %d", indexPath.row+1]];

  return cell;
}

API已稍微更改了规则,您无法再直接访问iPhoto图库。 相反,你得到这样的资产库URL。

assets-library://asset/asset.JPG?id=1000000003&ext=JPG

您使用ALAssetLibrary对象通过URL访问ALAsset对象。

所以从ALAssetLibrary的文档中将其抛入标题(或您的源代码)

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

这不是严格需要的,但保持美观。
然后在你的来源。

-(void)findLargeImage
{
    NSString *mediaurl = [self.node valueForKey:kVMMediaURL];

    //
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
            largeimage = [UIImage imageWithCGImage:iref];
            [largeimage retain];
        }
    };

    //
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
    };

    if(mediaurl && [mediaurl length] && ![[mediaurl pathExtension] isEqualToString:AUDIO_EXTENSION])
    {
        [largeimage release];
        NSURL *asseturl = [NSURL URLWithString:mediaurl];
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:asseturl 
                       resultBlock:resultblock
                      failureBlock:failureblock];
    }
}

需要注意的一点是,在我开始iOS4移植之前,这会使用对我来说不熟悉的块,但您可能希望看一下

https://www.mikeash.com/pyblog/friday-qa-2008-12-26.html

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html

他们弯曲了一下你的脑袋,但如果你认为它们是通知选择器或回调,它会有所帮助。

  • findLargeImage返回结果块​​时, findLargeImage没有作为回调运行。 所以largeImage还没有效果。
  • largeImage需要是一个不限定方法的实例变量。

我在使用该方法时使用此构造来执行此操作,但您可能会找到更适合您的方法。

[node.view findLargeImage];
UIImage *thumb = node.view.largeImage;
if (thumb) { blah blah }

这就是我在试图让这个工作时学到的东西。

iOS 5更新

使用iOS5和单核设备时,结果块触发的速度似乎有点慢,所以在调用findLargeImage后我无法直接使用该图像。 所以我把它改成了一个代表。

@protocol HiresImageDelegate <NSObject>
@optional
-(void)hiresImageAvailable:(UIImage *)aimage;
@end

和commecá

//
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
            UIImage *largeimage = [UIImage imageWithCGImage:iref];
            [delegate hiresImageAvailable:large];
        }
    };

沃伦的回答对我很有用。 对于某些人来说,一个有用的事情是同时包括图像方向和比例元数据。 您可以在结果块中执行此操作,如下所示:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) 
    {
        UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]];
        [delegate hiresImageAvailable:large];
    }
};

在这种情况下, imageWIthCGImage调用在为您创建UIImage时添加了比例和orientation

[UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]];

需要注意的一个技巧是,如果你在iOS 5上使用[rep fullScreenImage]而不是[rep fullResolutionImage] ,你会得到一张已经旋转过的图像 - 但它是在iPhone屏幕的分辨率下 - 即它的分辨率较低。

只是将Warren和oknox的答案组合成一个较短的片段:

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary assetForURL:self.selectedPhotos[i] resultBlock: ^(ALAsset *asset){
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    CGImageRef imageRef = [representation fullResolutionImage];
    if (imageRef) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        imageView.image = [UIImage imageWithCGImage:imageRef scale:representation.scale orientation:representation.orientation];
        // ...
    }
} failureBlock: ^{
    // Handle failure.
}];

我个人喜欢将我的failureBlock设置为nil

     NSURL* aURL = [NSURL URLWithString:@"URL here"];

     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
     [library assetForURL:aURL resultBlock:^(ALAsset *asset)
     {
     UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];

     cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage];
     }
     failureBlock:^(NSError *error)
     {
     // error handling
     NSLog(@"failure-----");
     }];

只需提供你在上面提供的photolibrary图像中获得的UIReferenceURl ......它的工作正常。 我把它浸透了

  • UIcollectionView单元格

    ..如果你只是想把它展示出来

  • 的UIImageView

    手段

更改

cell.backgroundView = [[UIImageView alloc] initWithImage:copyOfOriginalImage];

imageView.image = copyOfOriginalImage;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM