[英]How to save a Picture to a specific folder from React Native Camera Roll
[英]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.