iOS图片的压缩与上传

图片上传是每个项目中必不可少的一部分.而上传的时候如果不压缩的话,会对服务器造成较大的压力.也会有不好的用户体验.下面就和大家分享一下我自己用到的图片压缩与上传的方法.
1.项目要求要等图片比例压缩到1240*760以内

CGSize targetSize = image.size;
                            while (targetSize.width > 1240 && targetSize.height > 760) {
                                targetSize.width = targetSize.width / 2;
                                targetSize.height = targetSize.height / 2;
                            }
                            UIImage *newImage = [self imageCompressForSize:image targetSize:targetSize];
                            NSData *imageData = UIImageJPEGRepresentation(newImage, 0.1);

2.等比例压缩图片方法

#pragma mark 等比例压缩图片
-(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = size.width;
    CGFloat targetHeight = size.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    if(CGSizeEqualToSize(imageSize, size) == NO){
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }
        else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        if(widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }else if(widthFactor < heightFactor){
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(size);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

3.上传,用到AFNetworking(其中fileData传的就是图片数据)

#pragma mark 上传图片
+(void)uploadFileByString:(NSString *)urlString BodyDic:(NSDictionary *)bodyDic FileData:(NSData *)fileData FileName:(NSString *)fileName WithDataBlock:(void(^)(id data))dataBlock
{
    //1.字符串的转码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:urlString]];
    //2.创建管理者对象(session)
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //3.设置允许请求的类别
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", nil];
    
    
    [manager POST:urlString parameters:bodyDic constructingBodyWithBlock:^(id  _Nonnull formData) {
//        [formData appendPartWithFormData:fileData name:fileName];
        [formData appendPartWithFileData:fileData name:fileName fileName:@"image2.jpg" mimeType:@"image/jpeg"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"上传成功");
        dataBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传失败");
    }];
}

写的不好,大家凑合看哈.欢迎提建议

你可能感兴趣的:(iOS图片的压缩与上传)