如何从相机胶卷中选择一张图片,然后将其保存到我的应用程序的“支持文件”文件夹中,然后选择保存文件的名称?

Posted

技术标签:

【中文标题】如何从相机胶卷中选择一张图片,然后将其保存到我的应用程序的“支持文件”文件夹中,然后选择保存文件的名称?【英文标题】:How can I select a picture from the camera roll, then save it to the "Supporting Files" folder of my app, then pick up the name of the saved file? 【发布时间】:2015-03-20 20:12:25 【问题描述】:

我正在用 Objective-C 构建一个 ios 游戏,其中一部分是能够解锁新的“船”。现在,我有一些,但我想要一个“自定义船”选项,用户从他们的相机胶卷中选择一个图像,然后它在游戏中显示为一艘船。

所以,我想知道如何选择一张图片(不需要相机),然后将其保存到应用程序的 Supporting Files 文件夹中,以便游戏可以调用它。我还需要找出文件的名称,以便游戏调用正确的图像。

任何帮助将不胜感激。

我试过了

-(IBAction)chooseFromLibrary:(id)sender


UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];

// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];



//delegate method will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *dataImage = [[NSData alloc] init];
dataImage = UIImagePNGRepresentation(image);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

stringImage = customImage;

这是来自this *** 帖子的答案的略微修改的代码。

customImage 是放入 NSUserDefaults 以保存它的字符串,如下所示:

-(IBAction)enableCustom:(id)sender

NSString *playerSprite = customImage;
[[NSUserDefaults standardUserDefaults] setObject:playerSprite forKey:@"playerImage"];

NSString *playerImage = [[NSUserDefaults standardUserDefaults]
                         stringForKey:@"playerImage"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wow you unlocked custom" message:@"Fail lol" delegate:self cancelButtonTitle:@"ur mean" otherButtonTitles:@"haha ur an more poor then mi", nil];
[alert show];

所以,警报工作正常,但是当我尝试玩游戏时,它没有显示图像,而是显示一个带有红色大“X”的白色框。我不确定发生了什么。

【问题讨论】:

这太宽泛了。你试过什么?你有什么问题?将问题分解为离散的步骤。 @rmaddy 更新了帖子,提供了更多信息。问题是图像显示不正确,我只是想正确地重做整个事情。 imagePickerController:didFinishPickingMedia: 的前两行就可以了。其余的都关闭了。为什么要将图像的 PNG 数据转换为字符串?只需将数据写入文件即可。使用NSUserDefaults 是个坏主意。只需将图像写入文件即可。 @rmaddy 这就是问题所在。我不知道该怎么做。 NSUserDefaults 允许我将“[在此处插入图像名称].png/jpeg/whatever”保存在 plist 文件中,以便游戏可以轻松访问它。我不知道如何将图像写入文件,更不知道如何为游戏调用它。 见***.com/questions/28496019/… 【参考方案1】:

要选择图像并将其保存到自定义文件夹,请使用以下代码:

-(IBAction)chooseFromLibrary:(id)sender


UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];

// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];



//delegate methode will be called after picking photo either from camera or library
- (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:@"custom"];

    // Custom Images 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];

    customImage = fileName;


customImage 是我在头文件中设置为 IBOutlet NSString 的字符串变量。

然后在我的“启用”代码中,我将 customImage 字符串放在 NSUserDefaults 中,以便能够在游戏中访问它。该代码还包括一个带有两个按钮的警报。

-(IBAction)enableCustom:(id)sender

NSString *playerSprite = customImage;
[[NSUserDefaults standardUserDefaults] setObject:playerSprite forKey:@"playerImage"];

NSString *playerImage = [[NSUserDefaults standardUserDefaults]
                         stringForKey:@"playerImage"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wow you unlocked custom" message:@"Fail lol" delegate:self cancelButtonTitle:@"ur mean" otherButtonTitles:@"haha ur an more poor then mi", nil];
[alert show];

非常感谢用户 rmaddy 的帮助。他们将我推荐给this post,以防您需要。

【讨论】:

以上是关于如何从相机胶卷中选择一张图片,然后将其保存到我的应用程序的“支持文件”文件夹中,然后选择保存文件的名称?的主要内容,如果未能解决你的问题,请参考以下文章

保存用相机拍摄或从相机胶卷中选择的图像时出现长时间延迟 - iPhone

存储对相机胶卷图像的引用 - Swift 4

iOS:从相机胶卷中获取最后一张图片

科尔多瓦,从相机胶卷中获取所有照片

如何允许用户从他的相机胶卷或照片库中挑选照片?

如何避免将从相机胶卷中挑选的图像保存到相机胶卷? [iOS]