压缩图片分辨率

#pragma mark - 压缩图片分辨率
-(UIImage *)scaleImage: (UIImage *)image{
    
//    CGSize size = CGSizeMake(image.size.width * scaleFloat, image.size.height * scaleFloat);
//    
//    UIGraphicsBeginImageContext(size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGAffineTransform transform = CGAffineTransformIdentity;
//    
//    transform = CGAffineTransformScale(transform, scaleFloat, scaleFloat);
//    CGContextConcatCTM(context, transform);
//    
//    // Draw the image into the transformed context and return the image
//    [image drawAtPoint:CGPointMake(0.0f, 0.0f)];
//    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    return newimg;
//    CGSize newSize = CGSizeMake(600, 600);
    
    CGFloat scaleSize = 600;
    
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;

    if (width == 0 || height == 0){
        return image;
    }
    if(width < scaleSize && height < scaleSize){
        return image;
    }
    //压缩比例
    CGFloat scaleFactor;
    //图片长宽,数值大的压缩到600,剩下按长宽比压缩
    if (width > height) {
        scaleFactor = scaleSize / width;
    }else{
        scaleFactor = scaleSize / scaleSize;
    }
    
    CGFloat scaledWidth = width * scaleFactor;
    CGFloat scaledHeight = height * scaleFactor;
    CGSize targetSize = CGSizeMake(scaledWidth,scaledHeight);
    UIGraphicsBeginImageContext(targetSize);
    [image drawInRect:CGRectMake(0,0,scaledWidth,scaledHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

你可能感兴趣的:(压缩图片分辨率)