简体   繁体   English

如何从相机胶卷中选择图片,然后将其保存到应用程序的“ 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?

I'm building an iOS game in Objective-C, and part of it is the ability to unlock new "ships". 我正在Objective-C中构建一个iOS游戏,其中一部分是能够解锁新的“飞船”。 Now, I have a few, but I want a "custom ship" option, where the user selects an image from their camera roll, and then it displays as a ship in-game. 现在,我有一些,但是我想要一个“自定义飞船”选项,用户可以从其相机胶卷中选择一张图像,然后将其显示为游戏中的飞船。

So, I would like to know how to select a picture (don't need the camera), then save it to the Supporting Files folder in the app so the game can call it up. 因此,我想知道如何选择一张图片(不需要相机),然后将其保存到应用程序的Supporting Files文件夹中,以便游戏可以调用它。 I also need to find out the name of the file so the game can call the correct image. 我还需要找出文件的名称,以便游戏可以调用正确的图像。

Any help would be appreciated. 任何帮助,将不胜感激。

I have tried 我努力了

-(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;
}

Which is slightly modified code from an answer on this StackOverflow post. 这是稍微从回答修改后的代码这个 StackOverflow的职位。

customImage is the string that gets put into the NSUserDefaults to save it, as shown here: 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];
}

So, the alert works fine, but when I try to play the game, instead of showing the image, it just shows a white box with a large red "X" in it. 因此,警报效果很好,但是当我尝试玩游戏时,它没有显示图像,而是显示了一个带有大红色“ X”的白色框。 I'm not exactly sure what's up. 我不确定是怎么回事。

To pick the image and save it to a custom folder, use this code: 要选择图像并将其保存到自定义文件夹,请使用以下代码:

-(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 is a string variable I had set up as an IBOutlet NSString in the header file. customImage是我在头文件中设置为IBOutlet NSString的字符串变量。

Then in my "enable" code, I put the customImage string in NSUserDefaults in order to be able to access it in-game. 然后在我的“启用”代码中,将customImage字符串放入NSUserDefaults中,以便能够在游戏中对其进行访问。 The code also includes an alert with two buttons. 该代码还包括带有两个按钮的警报。

-(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];
}

Thanks so much to user rmaddy for their help. 非常感谢rmaddy用户的帮助。 They referred me to this post , in case you may need it. 他们推荐我这篇文章 ,以备不时之需。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将图片从 React Native Camera Roll 保存到特定文件夹 - How to save a Picture to a specific folder from React Native Camera Roll 如何在iPhone胶卷上取回保存的图片? - How do I get back a saved picture on iPhone camera roll? 如何将图像保存到相机胶卷? - How can I save an image to the camera roll? 用户可以像使用本机胶卷应用程序一样通过Safari将图像保存到我的应用程序吗? - Is there any way a user could save images to my app from safari as they can do with the native camera roll app? 如何在Swift中将视频从“ file:///”保存到相机胶卷中? - How to save videos from “file:///” to camera roll in Swift? 我可以在相机胶卷上获取视频的绝对 URI 并从我的应用程序中对其进行变异吗? - Can I get absolute URI of video on camera roll and mutate it from my app? 如何在iOS的弹出窗口中打开“相机胶卷”? - How can I open Camera Roll in pop up window in iOS? 如何拍摄带有徽标的图片并将其保存到相机胶卷 - how to take a picture with overlayed logo and save it to camera roll 从 iOS Safari 输入文件中拍摄的照片可以保存到相机胶卷吗? - Can a photo taken from iOS safari input file be saved to camera roll? 如何避免将从胶卷中拾取的图像保存到胶卷中? [iOS] - How can I avoid saving to camera roll an image picked from camera roll ? [iOS]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM