我想保存2张图片,因为它们被选中以分隔名称。
我使用本指南制作了我的uiimagepickercontroller:guide
基本上它是一个管理2个uiimageviews的uiimagepickercontroller,是否可以使用不同的名称保存2个不同的图像?或者我应该尝试使uiimagepickercontroller与众不同?
这是我的代码,它不适用于保存。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];
//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
switch (_selectedPhotoType) {
case imageView1Type:
imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
case imageView2Type:
imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage];
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{ }];
}
答案 0 :(得分:0)
看看这个实现。你需要的吗?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];
//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
switch (_selectedPhotoType) {
case imageView1Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo1.png"];
imageView1.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
break;
case imageView2Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo2.png"];
imageView2.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
break;
default:
break;
}
[webData writeToFile:imagePath atomically:YES];
[self dismissViewControllerAnimated:YES completion:^{ }];
}
答案 1 :(得分:0)
在@Heximal
的帮助下弄明白了- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo.png"];
//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
switch (_selectedPhotoType) {
case imageView1Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo1.png"];
imageView1.image = [info objectForKey:UIImagePickerControllerEditedImage];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
}
break;
case imageView2Type: {
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"latest_photo2.png"];
imageView2.image = [info objectForKey:UIImagePickerControllerEditedImage];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
}
}
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:^{ }];
}