如何从 ALAssetsLibrary ios 获取图像真实大小



我正在尝试从ALAssetsLibrary中选择所有真实尺寸的图像并保存在文档文件夹中。这是我从 ALAssetsLibrary 获取真实大小图像的代码。我收到以下错误。有没有其他方法可以从ALAssetsLibrary获取真实尺寸的图像?

与资产的连接中断或资产死亡

-(void)test{
    for (int i= 0; i < chosenImagesArray.count; i++) {
            NSDictionary *chosenImage = [self.chosenImages objectAtIndex:i];
                ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
                [assetLibrary assetForURL:[chosenImage valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset)
                 {
                     ALAssetRepresentation *rep = [asset defaultRepresentation];
                     unsigned long imageDataSize = (unsigned long)[rep size];
                     imageDataBytes = malloc(imageDataSize);
                     [rep getBytes:imageDataBytes fromOffset:0 length:imageDataSize error:nil];
                     NSData *data = [NSData dataWithBytesNoCopy:imageDataBytes length:imageDataSize freeWhenDone:YES];
                     UIImage *myImg =[UIImage imageWithData:data];
                     //Crop to Standard Size
                     UIImage *readyToWriteImage = [self croppIngimageByImageName:[self fixrotation:myImg ] ];
                     //Write To Document Directory
                     [self writeChosenImage:readyToWriteImage];


                 }
                 failureBlock:^(NSError *err) {
                                  NSLog(@"Error: %@",[err localizedDescription]);
                                 return ;
                  }];
            }
}
您可以

按照本教程"如何获取图像的元数据"进行操作,因为大小在图像的元数据中,因此您可以从返回的元数据字典中获取大小,这是链接 http://blog.codecropper.com/2011/05/getting-metadata-from-images-on-ios/

谢谢

最新更新