如何 select 相机胶卷中的图片,然后将其保存到我的应用程序的 "Supporting Files" 文件夹中,然后选择保存文件的名称?

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?

我正在 Objective-C 构建一个 iOS 游戏,其中一部分是解锁新 "ships" 的能力。现在,我有一些,但我想要一个 "custom ship" 选项,用户 select 从他们的相机胶卷中获取图像,然后它在游戏中显示为一艘船。

所以,我想知道如何select一张照片(不需要相机),然后将其保存到应用程序中的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 Whosebug post.

上的答案稍作修改的代码

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" 的白色框。我不太确定发生了什么。

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

-(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 的字符串变量。

然后在我的 "enable" 代码中,我将 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 的帮助。他们将我转介给 ,以备不时之需。