ios - 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 ios game in objective-c, , part of ability unlock new "ships". now, have few, want "custom ship" option, user selects image camera roll, , displays ship in-game.
so, know how select picture (don't need camera), save supporting files folder in app game can call up. need find out name of file game can call correct image.
any appreciated.
i have tried
-(ibaction)choosefromlibrary:(id)sender { uiimagepickercontroller *imagepickercontroller= [[uiimagepickercontroller alloc]init]; [imagepickercontroller setsourcetype:uiimagepickercontrollersourcetypephotolibrary]; // image picker needs delegate can respond messages [imagepickercontroller setdelegate:self]; // place image picker on screen [self presentviewcontroller:imagepickercontroller animated:yes completion:nil]; } //delegate method called after picking photo either 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 modified code answer on this stackoverflow post.
customimage string gets put nsuserdefaults save it, shown here:
-(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 unlocked custom" message:@"fail lol" delegate:self cancelbuttontitle:@"ur mean" otherbuttontitles:@"haha ur more poor mi", nil]; [alert show]; }
so, alert works fine, when try play game, instead of showing image, shows white box large red "x" in it. i'm not sure what's up.
to pick image , save custom folder, use code:
-(ibaction)choosefromlibrary:(id)sender { uiimagepickercontroller *imagepickercontroller= [[uiimagepickercontroller alloc]init]; [imagepickercontroller setsourcetype:uiimagepickercontrollersourcetypephotolibrary]; // image picker needs delegate can respond messages [imagepickercontroller setdelegate:self]; // place image picker on screen [self presentviewcontroller:imagepickercontroller animated:yes completion:nil]; } //delegate methode called after picking photo either 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 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 string variable had set iboutlet nsstring in header file.
then in "enable" code, put customimage string in nsuserdefaults in order able access in-game. code includes alert 2 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 unlocked custom" message:@"fail lol" delegate:self cancelbuttontitle:@"ur mean" otherbuttontitles:@"haha ur more poor mi", nil]; [alert show]; }
thanks user rmaddy help. referred me this post, in case may need it.
Comments
Post a Comment