Not able to get the saved data bytes when reading the same file
I am writing NSMutableData to image file using writeToFile with the below
code.
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
I am reading the saved image file with the below code.
-(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
}
If you see the above code, My source data length is '30506' before sending
it to writeToFile.
But I am not able to get the same bytes while reading the same image with
UIImagePickerController.
I tried with UIImagePNGRepresentation, UIImageJPEGRepresentation and
ALAssetsLibrary. But I am not able to get the same bytes in any of my
trials.
Can somebody guide me how to save/get the same bytes?
No comments:
Post a Comment