簡體   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