如何将 UIImagePicker 图片转换为字节数组 objective-c
How to convert UIImagePicker image into a byte array objective-c
我正在从图像选择器中选择 UIImage
。然后我想将我选择的图像作为字节数组传递给服务器。所以在这个委托中,我得到了我选择的图像的路径。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
filePath=info[UIImagePickerControllerReferenceURL];
然后转换成我喜欢的字节数组
NSData *data = [NSData dataWithContentsOfURL:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
NSLog(@"----BITE DATA----%@",byteData);
但是这个data
总是得到零。这是为什么?请帮我。
谢谢
请使用这个
UIImage *Image = (UIImage*)[info valueForKey:UIImagePickerControllerOriginalImage];
NSData *imgData = UIImageJPEGRepresentation(Image, 1); //1 it represents the quality of the image.
NSLog(@"Size of Image(bytes):%d",[imgData length]);
内部方法 didFinishPickingMediaWithInfo
使用以下代码获取图像。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(chosenImage)];
NSLog(@"image string : %@",info[UIImagePickerControllerEditedImage]);
// save using nsuserdefaults or any other your local database
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"imageData"];
}
现在使用以下代码将其显示到任何地方。
NSData *imageData = [[NSUserDefaults standardUserDefaults] valueForKey:@"imageData"];
UIImage *image = [UIImage imageWithData:imageData];
self.imageView.image = image;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData* imageData=UIImageJPEGRepresentation(image,1.0);
// Add selected imageData in Array and pass imageData to server
[imageDataArray addObject:imageData];
}
我正在从图像选择器中选择 UIImage
。然后我想将我选择的图像作为字节数组传递给服务器。所以在这个委托中,我得到了我选择的图像的路径。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
filePath=info[UIImagePickerControllerReferenceURL];
然后转换成我喜欢的字节数组
NSData *data = [NSData dataWithContentsOfURL:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
NSLog(@"----BITE DATA----%@",byteData);
但是这个data
总是得到零。这是为什么?请帮我。
谢谢
请使用这个
UIImage *Image = (UIImage*)[info valueForKey:UIImagePickerControllerOriginalImage];
NSData *imgData = UIImageJPEGRepresentation(Image, 1); //1 it represents the quality of the image.
NSLog(@"Size of Image(bytes):%d",[imgData length]);
内部方法 didFinishPickingMediaWithInfo
使用以下代码获取图像。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(chosenImage)];
NSLog(@"image string : %@",info[UIImagePickerControllerEditedImage]);
// save using nsuserdefaults or any other your local database
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"imageData"];
}
现在使用以下代码将其显示到任何地方。
NSData *imageData = [[NSUserDefaults standardUserDefaults] valueForKey:@"imageData"];
UIImage *image = [UIImage imageWithData:imageData];
self.imageView.image = image;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData* imageData=UIImageJPEGRepresentation(image,1.0);
// Add selected imageData in Array and pass imageData to server
[imageDataArray addObject:imageData];
}