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  _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

你可能感兴趣的:(iOS 上传图片 file格式 + 压缩图片)