Ios &引用;文件“;IMG_xxxx.JPG”;不能’;无法打开,因为没有此类文件。”;

Ios &引用;文件“;IMG_xxxx.JPG”;不能’;无法打开,因为没有此类文件。”;,ios,objective-c,uiimagepickercontroller,Ios,Objective C,Uiimagepickercontroller,我正在尝试将图像选择器中选择的图像传递给API。为此,我必须通过图像的完整路径。我现在做的是查找图像名和图像的文件路径,并将名称附加到文件路径上。即使在我将此完整路径传递给API之后,仍然会出现如下所述的错误: Error Domain=nscocaerordomain code=260“无法打开文件”IMG_0082.JPG,因为没有这样的文件。”UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/C7C8D

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

Error Domain=nscocaerordomain 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=nsposixerrordain=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];
-(void)imagePickerController:(UIImagePickerController*)选取器未使用信息完成PickingMediaWithInfo:(NSDictionary*)信息
{
//图像选择器
[picker DismissionViewControllerInitiated:是完成:无];
//分类图像
UIImage*image=[info objectForKey:@“UIImagePickerControllerEditedImage”];
//获取图像名称
NSURL*refURL=[info valueForKey:uiImagePickerController参考URL];
PHFetchResult*result=[PHAsset fetchAssetsWithALAssetURLs:@[refURL]选项:nil];
NSString*filename=[[result firstObject]filename];
//获取图像文件路径并在其上附加文件名
NSData*imageData=UIImagePNGRepresentation(图像);
NSString*path=[NSTemporaryDirectory()stringByAppendingPathComponent:filename];
[imageData WriteFile:原子路径:是];
NSDictionary*headers=@{@“内容类型”:@“多部分/表单数据;边界=----WebKitFormBoundary7MA4YWxkTrZu0gW”,
@“内容类型”:@“多部分/表单数据”,
@“接受”:@“应用程序/json”,
@“授权”:@“令牌60f6be2a21bdf731d86a8817b440a1afba692fed”,
@“缓存控制”:@“无缓存”,
@“邮差代币”:@“79ca63d8-1bdc-f3e3-13cd-9BC18B68CA10”};
NSArray*参数=@[{“名称”:@“任务”,@“值”:@“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”;
n错误*错误;
NSMutableString*body=[NSMutableString];
用于(NSDictionary*参数中的参数){
[正文格式:@--%@\r\n”,边界];
如果(参数[@“文件名”]){
[body appendFormat:@“内容处理:表单数据;名称=\“%@\”文件名=\“%@\”\r\n”、参数[@“名称”]、参数[@“文件名”];
[正文附录格式:@“内容类型:%@\r\n\r\n”,参数[@“内容类型”];
[body appendFormat:@“%@”,[NSString stringWithContentsOfFile:param[@“fileName”]编码:NSUTF8StringEncoding错误:&错误];
如果(错误){
NSLog(@“%@”,错误);
}
}否则{
[body appendFormat:@“内容处理:表单数据;名称=\“%@\”\r\n\r\n”,参数[@“名称”];
[正文格式:@“%@”,参数[@“值”];
}
}
[正文格式:@“\r\n--%@--\r\n”,边界];
NSData*postData=[body dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest*请求=[NSMutableUrlRequestRequestWithURL:[NSURL URLWithString:@]https://api.vize.ai/v1/classify/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[请求设置HttpMethod:@“POST”];
[请求setAllHTTPHeaderFields:headers];
[请求setHTTPBody:postData];
NSURLSession*会话=[NSURLSession sharedSession];
NSURLSessionDataTask*dataTask=[会话dataTaskWithRequest:request
completionHandler:^(NSData*数据,NSURLResponse*响应,NSError*错误){
如果(错误){
NSLog(@“%@”,错误);
}否则{
NSHTTPURLResponse*httpResponse=(NSHTTPURLResponse*)响应;
NSLog(@“%@”,httpResponse);
}
}];
[数据任务恢复];

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

所以这个

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} ];

让我试试,几分钟后再打给你。