首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ALAssetLibrary保存和加载图像

使用ALAssetLibrary保存和加载图像
EN

Stack Overflow用户
提问于 2013-12-02 00:41:26
回答 1查看 876关注 0票数 0

我把这个问题作为我之前发表的主题的续篇。

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

我的保存代码如下:

代码语言:javascript
复制
- (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保存到我的核心数据实体。现在我加载我的镜像,如下所示:

代码语言:javascript
复制
-(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:asset thumbnail ]加载缩略图,但图像仍然为空,也就是没有加载。然后我尝试加载fullResolutionImage。尽管如此,图像仍然是空的。

有什么解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2013-12-02 01:02:41

尝试像这样获取资产

代码语言:javascript
复制
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
}
];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20314525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档