AFN上传图片文件 遇到 NSURL中fileURLWithPath和URLWithString 的区别的问题
来源:互联网 发布:我爱你域名值钱吗 编辑:程序博客网 时间:2024/04/29 17:36
用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类型,对于它的内容没有任何的改变。
0 0
- AFN上传图片文件 遇到 NSURL中fileURLWithPath和URLWithString 的区别的问题
- fileURLWithPath:和URLWithString:的区别
- fileURLWithPath与 URLWithString的区别
- [IOS] URLWithString拼接NSURL时,url=nil的问题
- iOS中[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@""]]的使用
- NSURL URLWithString:url 中 中文字符问题(已解决)
- NSURL urlWithString 得到的对象为空的问题原因以及解决
- NSURL使用urlWithString得到的对象为nil的问题原因以及解决
- android中上传图片遇到的问题
- 【NSURL URLWithString:url 】中string不为空而URL输出为空的情况
- vue中上传文件遇到的问题
- struts文件上传和遇到的问题
- struts文件上传和遇到的问题
- 上传图片文件遇到的文件损坏问题
- 图片上传遇到的问题
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- C++程序(百元买百鸡、分出奇偶数、鸡兔同笼问题、选择排序法、整数奇偶排序)
- Codeigniter 的mysql的 limit 函数,和平常在mysql中不一致,因此修改。
- 基于JT/T808协议的车辆监控平台架构方案
- 软件测试思想者 之 “功能测试概念 - 单字解析”
- 分析一款实验室的木马
- AFN上传图片文件 遇到 NSURL中fileURLWithPath和URLWithString 的区别的问题
- javaweb学习总结(六)——Servlet开发(二)
- angularjs给我的感受
- linux下实时查看tomcat日志
- 谷歌教你5天搞定产品设计
- iOS一个插件icon maker 的使用
- kafka
- eclipse在linux下的快捷启动
- javaweb学习总结(七)——HttpServletResponse对象(一)