Objective c 读取同一文件时无法获取保存的数据字节
我正在使用writeToFile和下面的代码将NSMutableData写入图像文件Objective c 读取同一文件时无法获取保存的数据字节,objective-c,uiimagepickercontroller,nsdata,writetofile,Objective C,Uiimagepickercontroller,Nsdata,Writetofile,我正在使用writeToFile和下面的代码将NSMutableData写入图像文件 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *file = [documentsDirectory stringByAppendi
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
}
我正在用下面的代码读取保存的图像文件
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”,然后发送到WriteFile
但我无法在使用UIImagePickerController读取相同图像时获得相同的字节
我试过使用UIImagePNGRepresentation、uiimagejpegresentation和ALAssetsLibrary。但在我的任何试验中,我都无法获得相同的字节
有人能告诉我如何保存/获取相同的字节吗?这看起来很棘手:
[NSMutableData alloc]代码>。添加init
调用,看看它是否有什么不同。但是,既然您已经将数据作为NSData
对象([data appendData:rawData];
),为什么还要创建NSMutableData
)?稍后我将附加更多的NSData对象。这就是我声明NSMutableData的原因。我试过你的选择,但没有区别。