gpt4 book ai didi

iphone - 如何保存 UIImagePickerView Controller 返回的图像

转载 作者:行者123 更新时间:2023-12-03 20:24:53 25 4
gpt4 key购买 nike

我有一个应用程序,用户将从 UIImagePickerView 中选择图像。

从中选择图像后,我想将其保存在我的应用程序中。

如何实现这一目标?

预先感谢您对我的帮助。

最佳答案

假设您使用的是 SDK 3.0,以下是一些将图像保存到应用程序的文档文件夹中的代码:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Dismiss the picker
[[picker parentViewController] dismissModalViewControllerAnimated:YES];

// Get the image from the result
UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];

// Get the data for the image as a PNG
NSData* imageData = UIImagePNGRepresentation(image);

// Give a name to the file
NSString* imageName = "MyImage.png";

// Now, we have to find the documents directory so we can save it
// Note that you might want to save it elsewhere, like the cache directory,
// or something similar.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];

// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];

return;
}

关于iphone - 如何保存 UIImagePickerView Controller 返回的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1269214/

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