获取 iOS 图像的文件路径以在 Unity 中使用
Getting the file path of iOS image for use in Unity
我需要获取 iOS 图像的文件路径,以便将其用于我在 Unity 中处理的项目。
到目前为止我有这个代码:
#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);
}
但是 returns 图像地址的格式类似于以下内容:
assets-library://asset/asset.JPG?id=92C96E1D-F714-49E6-8074-5B5980FF944F&ext=JPG
据我所知,这仅对 iPhone 库有用,
有没有办法得到它的实际路径,或者我完全走错了路?
作为旁注,Unity 代码完全没问题,可以统一接收字符串等,但不知道如何处理它。
编辑:感谢 Pankaj 的回复,这个函数让我可以使用它
// 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);
}
从 PickerView 中获取图像,然后将其存储在文档目录中,然后将该路径用于 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);
我需要获取 iOS 图像的文件路径,以便将其用于我在 Unity 中处理的项目。 到目前为止我有这个代码:
#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);
}
但是 returns 图像地址的格式类似于以下内容:
assets-library://asset/asset.JPG?id=92C96E1D-F714-49E6-8074-5B5980FF944F&ext=JPG
据我所知,这仅对 iPhone 库有用, 有没有办法得到它的实际路径,或者我完全走错了路?
作为旁注,Unity 代码完全没问题,可以统一接收字符串等,但不知道如何处理它。
编辑:感谢 Pankaj 的回复,这个函数让我可以使用它
// 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);
}
从 PickerView 中获取图像,然后将其存储在文档目录中,然后将该路径用于 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);