I need to get a file path for an iOS image to use it for a project I'm working on in Unity. So far I have this code:
#pragma mark - UIImagePickerControllerDelegate
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info
{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
NSLog(@"type=%@",type);
if ([type isEqualToString:(NSString *)kUTTypeImage])
{
NSURL* imageURL = (NSURL *)[info valueForKey:UIImagePickerControllerReferenceURL];
NSString *urlString = [imageURL absoluteString];
const char* cp = [urlString UTF8String];
if(cp)
strcpy(image_path, cp);
else
{//If the image path is unasigned give ERROR instead
char* error = "ERROR";
strcpy(image_path, error);
}
}
[self dismissViewControllerAnimated:YES completion:NULL];
// UnitySendMessage("GameObject", "VideoPicked", video_url_path);
UnitySendMessage(callback_game_object_name, callback_function_name, image_path);
}
This however returns the image address in a format similar to the following:
assets-library://asset/asset.JPG?id=92C96E1D-F714-49E6-8074-5B5980FF944F&ext=JPG
Which as far as I can tell is only useful for iPhone libraries, is there a way to get its actual path, or am completely going down the wrong route?
As a side note the Unity code is completely fine, can get unity to receive the string etc. but it doesn't know what to do with it.
EDIT: This function got it to work for me thanks to Pankaj for his response
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"setName.png"];
//save image here and then use that imagePath
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *imageData = UIImagePNGRepresentation(editedImage);
[imageData writeToFile:imagePath atomically:YES];
}
NSLog(@"imagePath=%@",imagePath);
const char* cp = [imagePath UTF8String];
strcpy(image_path, cp);
[self dismissViewControllerAnimated:YES completion:NULL];
UnitySendMessage(callback_game_object_name, callback_function_name, image_path);
}
Get image from PickerView and then store it in document directory then use that path for Unity.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"setName.png"];
//save image here and then use that imagePath
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *imageData = UIImagePNGRepresentation(editedImage);
[imageData writeToFile:imagePath atomically:YES];
}
UnitySendMessage(callback_game_object_name, callback_function_name, imagePath);