上传图片到服务器 对图片进行压缩

iPhone拍照 图片还是有点大    对图片不进行处理  上传到服务器  经查询一张图片大小约1.8M左右   

因为一次性可能要上传几十张图片   1.8M一张图片   这明显有点大了

忘记从哪位网友那看来的,不过我项目中使用了,经过处理后图片一张大约200k吧

图片压缩代码   

.h

#import <UIKit/UIKit.h>

@interface UIImage (lxy)

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;

- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end

.m

#import "UIImage+lxy.h"

@implementation UIImage (lxy)

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}



- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else
            if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil){
    
    }
//        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}


我在项目中的使用方法

#pragma mark---ImagePickerControllerDelegate---
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    //通过判断picker的sourceType,如果是拍照则保存到相册去
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    
    
    CGSize size = image.size;
    if (size.width > 800) {
        
        size.height = size.height*800/size.width;
        size.width  = 800;
    } else if (size.width < 500) {
        size.width = 500;
        size.height = size.height;
    }
    
    image =  [[[UIImage alloc]init]scaleToSize:image size:size];
    
    
    
    SafeCheck *model = _dataArray[subNumber];
    [model.imageList addObject:image];
    _dataArray[subNumber] = model;
    
    
    
    NSString *title = model.imageList.count?[NSString stringWithFormat:@"已拍:%ld",(long)model.imageList.count]:@"拍照";
    [((SafeCheckCell *)[_mainTBV cellForRowAtIndexPath:[NSIndexPath indexPathForRow:subNumber inSection:0]]).photoBtn setTitle:title forState:UIControlStateNormal];

    [picker dismissViewControllerAnimated:NO completion:nil];
    
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
}




猜你喜欢

转载自blog.csdn.net/lixianyue1991/article/details/76127066