繁体   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?

我正在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;
}

这是稍微从回答修改后的代码这个 StackOverflow的职位。

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

然后在我的“启用”代码中,将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用户的帮助。 他们推荐我这篇文章 ,以备不时之需。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM