首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Iphone >

ALAssetsLibraryAssetForURLResultBlock 返回值解决方案

2012-09-23 
ALAssetsLibraryAssetForURLResultBlock 返回值//导出图片到指定路径-(UIImage *) exportImage:(NSURL *)

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;} 

热点排行