AFN上传图片文件 遇到 NSURL中fileURLWithPath和URLWithString 的区别的问题

5 篇文章 0 订阅
4 篇文章 0 订阅

用AFN上传本地文件的时候遇到的本地文件路径的问题

//MARK:2.66 H5图片上传接口
- (void)uploadImageWithImageFile:(UIImage *)imageFile imageName:(NSString *)imageName complete:(void(^)(id model))completeBlock failure:(void(^)())failureBlock {

    NSString *url = SERVER_INTERFACE_HTML_UPLOADIMAGE;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:0];
//    [parameters setValue:imageFile forKey:@"imagefile"];
    [parameters setValue:imageName forKey:@"imagename"];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    WEAKSELF
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //        NSURL *fileUrl = [NSURL URLWithString:@"/Users/liliansi/Desktop/testshot.png"];

//        [formData appendPartWithFileURL:[NSURL URLWithString:fileUrl] name:@"imagefile" fileName:imageName mimeType:@"image/jpg" error:NULL];
//        [formData appendPartWithFileData:[NSData dataWithContentsOfFile:fileUrl] name:@"imagefile" fileName:imageName mimeType:@"image/jpg"];

        VcUser *user = [[VcUserManager sharedInstance] currentUser];
        NSString *path = [[[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"users"] stringByAppendingPathComponent:user.userID.stringValue] stringByAppendingPathComponent:self.productid.stringValue];
        BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        NSAssert(bo,@"创建目录失败");
        NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imageName]];
        NSURL *fileURL = [NSURL fileURLWithPath:filePath];

//        NSString *path1 = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//        NSBundle *bundle = [NSBundle bundleWithPath:path];
//        NSURL *fileURL = [bundle URLForResource:imageName withExtension:nil];
//        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:imageName];
//        NSURL *fileURL = [NSURL URLWithString:filePath];
        NSLog(@"\npath: %@\nimageName: %@",path,imageName);
        NSLog(@"\nfileURL: %@",fileURL);
        [formData appendPartWithFileURL:fileURL name:@"imagefile" error:NULL];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@", [operation responseString]);
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSString* code = [dict objectForKey:@"code"];
        if (code == nil || ![code isEqualToString:SERVER_RESPONSE_CODE_SUCCESS]) {

            NSString* errDesc = [dict objectForKey:@"desc"];
            [weakSelf.view makeToast:errDesc duration:2.0 position:@"bottom"];
            if (failureBlock) {
                failureBlock();
            }
            return;
        }
        NSDictionary *imageinfo = [dict objectForKey:@"imageinfo"];
        if (imageinfo && ![imageinfo isKindOfClass:[NSNull class]]) {
            VcImageModel *pictureModel = [[VcImageModel alloc]initWithAttributesDictionary:imageinfo];

            if (completeBlock) {
                completeBlock(pictureModel);
            }
        }
        NSLog(@"上传完成");

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"上传失败->%@", error);
        NSLog(@"错误 %@", error.localizedDescription);
        if (failureBlock) {
            failureBlock();
        }
    }];

}

问题以及总结:
初始化URL,其中 filePath 是一个本地的文件路径。
NSURL *appURL = [NSURL URLWithString:filePath];

在真机上可以初始化appURL,但模拟器上appURL为nil;

正确初始化一个本地文件的URL,应使用下边的方法,真机和模拟器都没问题

NSURL *appURL = [NSURL fileURLWithPath:url];

做实验:
NSString *str=@”http://www.9yunping.com/index“;
NSURL *url1=[NSURL fileURLWithPath:str];
NSURL *url2=[NSURL URLWithString:str];
NSLog(@”url1=%@”,url1);
NSLog(@”url2=%@”,url2);
输出结果为:
url1=http:/www.9yunping.com/index – file:///
url2=http://www.9yunping.com/index
通过上面的输出结果可知:
fileURLWithPath是将str转化为文件路径,可以自动的去掉“/”。而URLWithString紧紧是将url2转化成NSURL类型,对于它的内容没有任何的改变。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值