使用ALAssetsLibrary和ALAsset取出Image作为NSData [英] Using ALAssetsLibrary and ALAsset take out Image as NSData

查看:144
本文介绍了使用ALAssetsLibrary和ALAsset取出Image作为NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望直接以NSData对象的形式使用ALAssetsLibrary和ALAsset提取图像。

I wish to extract the image using ALAssetsLibrary and ALAsset directly in the form of a NSData object.

使用NSURL我按以下方式取出图像。

Using a NSURL I take out the image in the following manner.

NSURL *referenceURL =newURL;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset)
{
     UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
}

现在我们将图像作为UIImage拍摄,但我需要拍摄直接将图像作为NSData。

Now here we take the image as a UIImage, but I need to take the image directly as NSData.

我希望这样做是因为(我已经读过)一旦你在UIImage中拍摄图像,那么我们就会失去图像的所有EXIF细节。

I wish to do this because (I have read that) once you take the image in UIImage, then we lose all the EXIF details of the Image.

这就是我想直接将图像提取为NSData而不是这样做的原因

That's the reason I want to extract the image directly as NSData, instead of doing this

NSData *webUploadData=UIImageJPEGRepresentation(copyOfOriginalImage, 0.5);

这一步让我失去了所有的EXIF细节。

This step makes me lose all the EXIF details.

请帮助。

推荐答案

        ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
        [assetLibrary assetForURL:[[self.imagedata objectAtIndex:i] resultBlock:^(ALAsset *asset) 
        {
            ALAssetRepresentation *rep = [asset defaultRepresentation];
            Byte *buffer = (Byte*)malloc(rep.size);
            NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
            NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
            [data writeToFile:photoFile atomically:YES];//you can save image later
        } 
        failureBlock:^(NSError *err) {
            NSLog(@"Error: %@",[err localizedDescription]);
        }];

这篇关于使用ALAssetsLibrary和ALAsset取出Image作为NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆