【问题标题】:Saving and loading images using ALAssetLibrary使用 ALAssetLibrary 保存和加载图像
【发布时间】:2013-12-01 16:41:25
【问题描述】:

我发布这个问题是我之前发布的以下主题的延续。

我正在我的 ios 应用程序中捕获图像和视频并将其保存到相机胶卷(客户要求)。但是,要查看捕获的图像/视频,我将核心数据中的assetURL 保存为字符串。

我的保存代码如下:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

   UIImage *cameraMedia = [info  objectForKey:UIImagePickerControllerOriginalImage ];
   NSDictionary* metaData = [info objectForKey:UIImagePickerControllerMediaMetadata];

   NSString* mediaType = [info objectForKey: UIImagePickerControllerMediaType];
   NSURL* mediaUrl = (NSURL*)[info valueForKey:UIImagePickerControllerMediaURL];

   ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];

   //For now we are saving image capture time from here
   //In future we may need to load the time from image metaData
   NSDate *captureTime = [[NSDate alloc] init];

   //completion blocks
   ALAssetsLibraryWriteImageCompletionBlock imageCompletionBlock = ^(NSURL* imgURL, NSError* error){
    if (error == nil) {
        NSLog(@"Asset URL %@", imgURL);
        [self saveEvidence:imgURL mediaType:mediaType withDate:captureTime];

       } else {
           UIAlertView *alertView =
        [[UIAlertView alloc] initWithTitle:@"Failed to save image to device!"
                                   message:[error localizedDescription]
                                  delegate:self
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:Nil];
        [alertView show];
    }

};

ALAssetsLibraryWriteVideoCompletionBlock videoCompletionBlock = ^(NSURL* videoURL, NSError* error){
    if(error == nil)
    {
        NSLog(@"Asset URL %@", videoURL);
        [self saveEvidence:videoURL mediaType:mediaType withDate:captureTime];
    }else
    {
        UIAlertView *alertView =
        [[UIAlertView alloc] initWithTitle:@"Failed to save video to device!"
                                   message:[error localizedDescription]
                                  delegate:self
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:Nil];
        [alertView show];
    }

};


    if(CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){
        [al writeImageToSavedPhotosAlbum:[cameraMedia CGImage] metadata:metaData completionBlock: imageCompletionBlock];

    }else if(CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie , 0) == kCFCompareEqualTo){
        [al writeVideoAtPathToSavedPhotosAlbum:mediaUrl completionBlock:videoCompletionBlock];

   }

       [self dismissViewControllerAnimated:YES completion:nil];
  }

在 saveEvidence 中,我将 URL 保存到我的核心数据实体。现在我正在按如下方式加载我的图像:

-(void)loadThumbNail:(NSString*) mediaURL{

NSLog(@"image URL String: %@", mediaURL);

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset)
{
    NSLog(@"in result block");
    //self.thumbNailImage = [UIImage imageWithCGImage:[asset thumbnail]];

    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) {
        self.thumbNailImage = [UIImage imageWithCGImage:iref];
    }
};

//
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error)
{
    NSLog(@"Cant load image - %@",[error localizedDescription]);
};

if(mediaURL && [mediaURL length])
{
    self.thumbNailImage = nil;

    NSURL *asseturl = [NSURL URLWithString:mediaURL];
    NSLog(@"image URL: %@", asseturl);

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];

    [assetslibrary assetForURL:asseturl
                   resultBlock:resultblock
                  failureBlock:failureblock];
   }

}

最初我尝试使用以下方法加载缩略图: self.thumbNailImage = [UIImage imageWithCGImage:[资产缩略图]]; 但是图像仍然为零,即未加载。然后我尝试加载 fullResolutionImage。 仍然,图像为零。

有什么办法吗?

【问题讨论】:

  • 如果您仍然遇到任何问题,请在此处更新...
  • 我认为我的技术奏效了。可能是关于异步图像加载的问题。我在结果块中得到非零图像引用,但在 cellForItemAtIndexPath 中,在单元格图像视图中加载图像时,我得到零。这是我的调用顺序,[self loadThumbNail:evidence.url]; [cell.imageThumNailView setImage:self.thumbNailImage];经过进一步调查,我发现在图像加载调用之前调用了将图像分配给单元格。

标签: ios iphone objective-c ios6 alassetslibrary


【解决方案1】:

尝试获取这样的资产

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
 [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
  {
      if (alAsset)
      {
          ALAssetRepresentation *representation =[alAsset defaultRepresentation];
          NSURL *url = [representation url];

          if([url isEqualToString:YOUR_SAVED_URL])
           {
            NSString *assetType=[alAsset valueForProperty:ALAssetPropertyType];
            UIImage *thumbNailImage=[UIImage imageWithCGImage:alAsset.thumbnail];
           }
      }
  }];
 if(group==nil)
 {
     // do what ever if group getting null
 }
} failureBlock: ^(NSError *error)
{
 // may be photo privacy setting disabled
}
];

【讨论】:

  • 我猜,您的方法将从相机胶卷中获取所有图像。但我只需要获取从应用程序拍摄的照片。因此,我将 URL 保存在我的核心数据中。
  • 那么您需要将所有保存的 url 与当前资产 url 检查更新的 Ans 进行比较。
猜你喜欢
  • 2011-12-11
  • 2020-01-07
  • 2018-11-03
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多