gpt4 book ai didi

ios - 如何从 UIImagePickerController 将图像保存到应用程序文件夹?

转载 作者:行者123 更新时间:2023-11-29 02:18:09 25 4
gpt4 key购买 nike

我有一个让我抓狂的问题。我已经搜索了很多来解决它,但我发现的所有答案都是旧的并且没有帮助。

我目前正在开发使用 UIImagePickerControllerDelegatedidFinishPickingMediaWithInfo 的应用程序,我想将所选图像保存到应用程序文件夹。

到目前为止,我可以用它来选择图像;

- (IBAction)btnPressed:(UIButton *)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
imgPicker.allowsEditing = NO;

[self presentViewController:imgPicker animated:YES completion:nil];
}

}

但无法使用 didFinishPickingMediaWithInfo 将其保存到应用程序中;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

//Assigning the selected image to imageView to see on UI side.
imageViewerImage.image = image;
}
}

我想知道这一点之后应该是什么部分。

非常感谢您提供的任何帮助。

最佳答案

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

[self dismissViewControllerAnimated:YES completion:NULL];

UIImage* image;

if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"])
{

image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New Folder"];

// New Folder is your folder name

NSError *error = nil;

if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];

NSData *data = UIImageJPEGRepresentation(image, 1.0);

[data writeToFile:fileName atomically:YES];

}
}

关于ios - 如何从 UIImagePickerController 将图像保存到应用程序文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28496019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com