使用ALAssetsLibrary和ALAsset取出Image作为NSData

Shi*_*tty 18 iphone exif uiimage nsdata uiimagejpegrepresentation

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

使用NSURL我以下列方式取出图像.

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

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

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

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

NSData *webUploadData=UIImageJPEGRepresentation(copyOfOriginalImage, 0.5);
Run Code Online (Sandbox Code Playgroud)

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

请帮忙.

Dha*_*wal 33

        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]);
        }];
Run Code Online (Sandbox Code Playgroud)

  • 我在同一时间导入多个ALAsset时遇到此技术的问题,似乎缓冲区被重用于下一个项目. (2认同)