当前位置: 代码迷 >> Iphone >> ALAssetsLibraryAssetForURLResultBlock 返回值解决方案
  详细解决方案

ALAssetsLibraryAssetForURLResultBlock 返回值解决方案

热度:89   发布时间:2016-04-25 06:21:28.0
ALAssetsLibraryAssetForURLResultBlock 返回值
//导出图片到指定路径
-(UIImage *) exportImage:(NSURL *) assetPhotoURL: (int) imageType
{
  ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
  {
  CGImageRef imageRef;
   
  if (imageType == SIZE_TYPE_THUMBNAIL)
  {
  imageRef = [myasset thumbnail];
  }
  else
  {
  ALAssetRepresentation *assetRepresentation = [myasset defaultRepresentation]; //大图片
  if (imageType == SIZE_TYPE_FULL_SCREEN)
  imageRef = [assetRepresentation fullScreenImage];
  else if (imageType == SIZE_TYPE_FULL_RESOLUTION)
  imageRef = [assetRepresentation fullResolutionImage];
  }
   
  if(imageRef)
  {
  UIImage *image = [UIImage imageWithCGImage:imageRef];
  NSLog(@"获取图片成功");

  //如何在获取图片后返回
  }
  };
   
  ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error)
  {
  NSLog(@"获取图片失败");
  };
   
  //获取图片
  ALAssetsLibrary *assetsLibrary = [[[ALAssetsLibrary alloc] init] autorelease];
  [assetsLibrary assetForURL:assetPhotoURL resultBlock:resultblock failureBlock:failureblock];
   
  return nil;
}


上面的代码可以获取到资源库中指定url的图片对象,但如何修改代码让其在获取到UIImage对象后可以直接返回,而不是通过委托进行返回?

------解决方案--------------------
可以使用关键字__block,声明一个变量,在block内改变其值:
C/C++ code
-(UIImage *) exportImage:(NSURL *) assetPhotoURL: (int) imageType{__block UIImage *image = nil; ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){......image = xxx;}return image;}
  相关解决方案