0

我正在使用带有以下代码的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读取相同的图像时,我无法获得相同的字节。

我尝试使用UIImagePNGRepresentationUIImageJPEGRepresentationALAssetsLibrary。但我无法在任何试验中获得相同的字节。

有人可以指导我如何保存/获取相同的字节吗?

4

0 回答 0