gpt4 book ai didi

ios - "The file “IMG_xxxx.JPG” 无法打开,因为没有这样的文件。”

转载 作者:行者123 更新时间:2023-11-29 00:06:00 25 4
gpt4 key购买 nike

我正在尝试将在图像选择器中选择的图像传递给 API。为此,我必须传递图像的完整路径。我目前正在做的是找到图像名称和图像的文件路径,并将名称附加到文件路径上。即使在我将此完整路径传递给 API 之后,我仍然收到如下所述的错误:

Error Domain=NSCocoaErrorDomain Code=260 “无法打开文件“IMG_0082.JPG”,因为没有这样的文件。” UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/C7C8D8A8-B9E7-4E4E-B35C-DBFB061804D6/tmp/IMG_0082.JPG, NSUnderlyingError=0x14fb200f0 {Error Domain=NSPOSIXErrorDomain Code=2 "没有这样的文件或目录”}}

问题是什么?我在下面附上了完整的 imagePickerController 方法。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
// Dismiss image picker
[picker dismissViewControllerAnimated:YES completion:nil];

// Classify image
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

// Get image name
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
NSString *filename = [[result firstObject] filename];

// Get image file path and append file name onto it
NSData *imageData = UIImagePNGRepresentation(image);
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[imageData writeToFile:path atomically:YES];

NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
@"Content-Type": @"multipart/form-data",
@"Accept": @"application/json",
@"Authorization": @"Token 60f6be2a21bdf731d86a8817b440a1afba692fed",
@"Cache-Control": @"no-cache",
@"Postman-Token": @"79ca63d8-1bdc-f3e3-13cd-9bc18b68caa0" };
NSArray *parameters = @[ @{ @"name": @"task", @"value": @"4b363547-58da-48a8-b76d-8877dd816d13" },
@{ @"name": @"image_file", @"fileName": @"/private/var/mobile/Containers/Data/Application/C7C8D8A8-B9E7-4E4E-B35C-DBFB061804D6/tmp/IMG_0082.JPG" } ];
NSString *boundary = @"----WebKitFormBoundary7MA4YWxkTrZu0gW";

NSError *error;
NSMutableString *body = [NSMutableString string];
for (NSDictionary *param in parameters) {
[body appendFormat:@"--%@\r\n", boundary];
if (param[@"fileName"]) {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
[body appendFormat:@"Content-Type: %@\r\n\r\n", param[@"contentType"]];
[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];
if (error) {
NSLog(@"%@", error);
}
} else {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
[body appendFormat:@"%@", param[@"value"]];
}
}
[body appendFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.vize.ai/v1/classify/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
}
}];
[dataTask resume];

最佳答案

数组中的第二个参数不应该也是您写入文件的位置吗?

所以这个...

NSArray *parameters = @[ @{ @"name": @"task", @"value": @"4b363547-58da-48a8-b76d-8877dd816d13" },
@{ @"name": @"image_file", @"fileName": @"/private/var/mobile/Containers/Data/Application/C7C8D8A8-B9E7-4E4E-B35C-DBFB061804D6/tmp/IMG_0082.JPG" } ];

应该是

NSArray *parameters = @[ @{ @"name": @"task", @"value": @"4b363547-58da-48a8-b76d-8877dd816d13" },
@{ @"name": @"image_file", @"fileName": path} ];

关于ios - "The file “IMG_xxxx.JPG” 无法打开,因为没有这样的文件。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48062900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com