Xcode-从照片库中保存图像以供进一步使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14051065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:24:12  来源:igfitidea点击:

Xcode-Saving the image from the photo library for further use

iphoneobjective-cxcodeuiimagepickercontroller

提问by Mikeazio

I am trying to make a simple application using XCode 4.5 which would allow the user to chose any particular image via accessing his photo library, then submit that image for recognition with the help of the Tesseract library.

我正在尝试使用 XCode 4.5 制作一个简单的应用程序,它允许用户通过访问他的照片库来选择任何特定的图像,然后在 Tesseract 库的帮助下提交该图像进行识别。

The problem is that I do not know how to further save the selected user picture, In other words, I can provide the user with an option for going in the picture library and let him chose a picture, but I do not how to then save that selected picture so that it can be further processed.

问题是我不知道如何进一步保存选择的用户图片,换句话说,我可以为用户提供进入图片库的选项,让他选择一张图片,但我不知道如何保存选择的图片,以便它可以进一步处理。

I hope i made it clear, Appreciate your help.

我希望我说清楚了,感谢您的帮助。

回答by Kalpesh

Try this, Save image:

试试这个,保存图像:

NSString *imgName=[@"imgname.png"];
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

Retrive image:

检索图像:

NSString *imgName= [[NSUserDefaults standardUserDefaults]valueForKey:@"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,imageName];
[imageview setImage:[UIImage imageWithContentsOfFile:filePath]];

回答by Bhavin

I am still not clear about what exactly you want. You can always Save Imagein or Retrieve Imagefrom Photo Library.

我仍然不清楚你到底想要什么。您始终可以在照片库中保存图像或从照片库中检索图像

回答by akdsouza

What you need to do is save the image data after selection inside the following delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {You can get the image via the key UIImagePickerControllerOriginalImagein the info dictionary and possibly assign it to an imageview or save it as mentioned by @Kalpesh. Check out apple's documentationfor more info. Also check out this good tutorialthat shows how to pick photos from the photo album or the camera.

您需要做的是在以下委托中选择后保存图像数据- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {您可以通过UIImagePickerControllerOriginalImage信息字典中的键获取图像,并可能将其分配给图像视图或按照@Kalpesh 所述进行保存。查看苹果的文档以获取更多信息。还可以查看这个很好的教程,它展示了如何从相册或相机中挑选照片。