iOS 上传图片 file格式 + 压缩图片

之前用OSS直接上传 在重新上报URL 看到file格式上传有点蒙蔽

- (void)uploadImgFile:(UIImage *)imageFile result:(void(^)(BOOL succeed, NSDictionary *dic))complete{    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",root,uploadPicture];
    
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSData *data = [self imageZipToData:imageFile];
        
        User *user = [SaveObject getModelWithKey:@"user"];
        NSString *fileStr = [NSString stringWithFormat:@"%@%@.png",user.ID, [self getTimeNow]];
     
##    formData  name:你的入参字典的key
       [formData appendPartWithFileData:data name:@"file" fileName:fileStr mimeType:@"image/png"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"成功");
        NSDictionary *result = responseObject;
        
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSString *success = [NSString stringWithFormat:@"%@", result[@"success"]];
        
        if ([success isEqualToString:@"1"]) {
            
            complete(YES, result);
            
        } else {
            
            NSString *errorsStr = result[@"errorDesc"];
            [SimpleAlertView showWithState:errorsStr];
            
            complete(NO, nil);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败");
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        NSDictionary *userinfo = error.userInfo;
        NSString *errorsStr = [[NSString alloc] initWithData:userinfo[@"com.alamofire.serialization.response.error.data"]  encoding:NSUTF8StringEncoding];
        NSLog(@"请求失败:%@", errorsStr);
        if (errorsStr.length > 0) {
            [SimpleAlertView showWithState:errorsStr];
        }else{
            complete(NO, nil);
        }

    }];

}

图片名字的随机数

/**
 *  返回当前时间
 */
- (NSString *)getTimeNow
{
    NSString* date;
    NSDateFormatter * formatter = [[NSDateFormatter alloc ] init];
    [formatter setDateFormat:@"YYYYMMddhhmmss"];
    date = [formatter stringFromDate:[NSDate date]];
    //取出个随机数
    int last = arc4random() % 10000;
    NSString *timeNow = [[NSString alloc] initWithFormat:@"%@-%i", date,last];
    NSLog(@"%@", timeNow);
    return timeNow;
}

压缩图片

/**
 *  压缩图片
 */
- (NSData *)imageZipToData:(UIImage *)newImage{
    
    NSData *data = UIImageJPEGRepresentation(newImage, 1.0);
    
    if (data.length > 500 * 1024) {
        
        if (data.length>1024 * 1024) {//1M以及以上
            
            data = UIImageJPEGRepresentation(newImage, 0.5);
            
        }else if (data.length>512*1024) {//0.5M-1M
            
            data=UIImageJPEGRepresentation(newImage, 0.6);
            
        }else if (data.length>200*1024) { //0.25M-0.5M
            
            data=UIImageJPEGRepresentation(newImage, 0.9);
        }
    }
    return data;
}


扩展

我的报讯信息是:reason: 'Invalid type in JSON write (UIImage)

网上查了下,能够转换为json字符串的对象必须具有如下属性:

顶层对象必须是NSArray或者NSDictionary

所有的对象必须是NSString/NSNumber/NSArray/NSDictionary/NSNull的实例

所有NSDictionary的key必须是NSString类型

数字对象不能是非数值或无穷;

相关报错信息: reason: 'Invalid type in JSON write (NSConcreteMutableData)

[iOS]将含有NSData数据的数组转化为json字符串报错:reason: 'Invalid type in JSON write (NSConcreteMutableData)
http://blog.csdn.net/lqq200912408/article/details/50531862

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,203评论 30 471
  • 军训第七天,整个人都非常疲惫,躺在床上五分钟就能睡着的那种,累归类,但还是很快乐,和一群新伙伴们相处的很好。一段...
    Edway阅读 133评论 0 0
  • 战隼128阅读 217评论 0 0
  • 今天我来回答大家的一个疑问,也是大家的一个问题,那就是UZI凭什么被称为世界顶级的ADC?最近UZI在我们LPL自...
    黄铜刀阅读 509评论 0 0
  • 这冬天如你所愿,凄美绝伦 天上的水如棉花一般蓬松,地面的水凝固 我们踩在松软的水面,感受水的硬度 城市的霓虹灯,摇...
    代晓阅读 326评论 0 1