我正在使用 writeToFile 使用以下代码将 NSMutableData 写入图像文件。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"binaryData.jpg"];
NSMutableData *data = [NSMutableData alloc];
[data appendData:rawData];
[data writeToFile:file atomically:NO];
NSLog (@"Data Length: %d.",data.length);
// Data Length: 30506
我正在使用以下代码阅读已保存的图像文件。
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *selectedImageFile = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *PNGData = [NSData dataWithData:UIImagePNGRepresentation(selectedImageFile)];
NSData *JPEGData = [NSData dataWithData:UIImageJPEGRepresentation(selectedImageFile, 1.0)];
NSURL *imageURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library assetForURL:imageURL resultBlock:^(ALAsset *asset) {
Byte *buffer = (Byte*)malloc(asset.defaultRepresentation.size);
NSUInteger k = [asset.defaultRepresentation getBytes:buffer fromOffset: 0.0 length:asset.defaultRepresentation.size error:nil];
rawData = [NSData dataWithBytesNoCopy:buffer length:k freeWhenDone:YES];
}
NSLog (@"PNG Length: %d.", PNGData.length); // PNG Length: 78101
NSLog (@"JPEG Length: %d.", JPEGData.length); // JPEG Length: 37548
NSLog (@"Raw Data Length: %d.", PNGData.length); // Raw Data Length: 34163
}
如果您看到上述代码,我的源数据长度为'30506',然后再将其发送到 writeToFile 。
但是使用 UIImagePickerController 读取相同的图像时,我无法获得相同的字节。
我尝试了 UIImagePNGRepresentation , UIImageJPEGRepresentation 和 ALAssetsLibrary 。但是我在任何试验中都无法得到相同的字节。
有人可以指导我如何保存/获取相同的字节吗?