1
我需要获取iOS图像的文件路径,以将其用于我在Unity中处理的项目。 到目前为止,我有这样的代码:获取用于Unity的iOS图像的文件路径
#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);
}
然而,这返回一个格式的图像地址类似以下内容:
assets-library://asset/asset.JPG?id=92C96E1D-F714-49E6-8074-5B5980FF944F&ext=JPG
其中,据我可以告诉仅供iPhone库有用, 有没有办法得到它的实际路径,还是完全走错了路线?
作为一个方面说明,Unity代码是完全正常的,可以得到统一接收字符串等,但它不知道如何处理它。
编辑:这个功能得到了它为我感谢来自PickerView工作潘卡他的回应
// 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);
}
也许我不是专家,但在这里看不到任何C#代码。 –
如果您从pickerView中获取图像,则可以将该图像存储在该目录中,然后可以将该目录路径用于Unity工作。 – Pankaj