iOS--圖片壓縮

我們可以在不減小圖片的分辨率(質(zhì)量可以適當減?。┑那闆r下,顯著減小圖片的大小

float kCompressionQuality = 0.3; // 具體大小自己調(diào)
NSData *photo = UIImageJPEGRepresentation(UIImage, kCompressionQuality);

上面方法等價于下面: 壓縮圖片質(zhì)量

+(UIImage *)reduceImage:(UIImage *)image percent:(float)percent
{
    NSData *imageData = UIImageJPEGRepresentation(image, percent);
    UIImage *newImage = [UIImage imageWithData:imageData];
    return newImage;
}

//將圖片壓縮到指定比例

-(UIImage *)scaleToSize:(UIImage *)aImage size:(CGSize)size{
    
    //創(chuàng)建context,并將其設置為正在使用的context
    UIGraphicsBeginImageContext(size);
    //繪制出圖片(大小已經(jīng)改變)
    [aImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //獲取改變大小之后的圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //context出棧
    UIGraphicsEndImageContext();
    return newImage; //返回獲得的圖片
}

等比例壓縮

//等比例壓縮
-(UIImage *)oldImage:(UIImage *)oldImage toSize:(CGSize)size{
    
    UIImage *newImage = nil;//新照片對象
    CGSize theSize = oldImage.size;//壓縮前圖片size
    
    CGFloat width = theSize.width; //壓縮前圖片width
    CGFloat height = theSize.height;//壓縮前圖片height
    
    CGFloat newWidth = size.width; //壓縮后圖片width
    CGFloat newHeight = size.height;//壓縮后圖片height
    
    CGFloat scaleFactor = 0.0;//初值
    
    CGFloat toWidth = newWidth;//壓縮后圖片width
    CGFloat toHeight = newHeight;//壓縮后圖片height
    
    CGPoint thumnailPoint = CGPointMake(0.0, 0.0);//給初值

    if (CGSizeEqualToSize(theSize, size) == NO) {
        //判斷是不是已經(jīng)滿足 theSize = size 要求
        
        CGFloat widthFac = newWidth/width;
        CGFloat heithrFac = newHeight/height;
        if (widthFac > heithrFac) {
            scaleFactor = widthFac;
        }else {
            scaleFactor = heithrFac;
        }
        //不滿足做等比例縮小處理
        toWidth = width *scaleFactor;
        toHeight = height *scaleFactor;
        
        if (widthFac > heithrFac) {
            thumnailPoint.y = (newHeight - toHeight)* 0.5;
        }else if (widthFac < heithrFac){
            thumnailPoint.x = (newWidth - toWidth)* 0.5;
        }
    }
    
    //創(chuàng)建context,并將其設置為正在使用的context
    UIGraphicsBeginImageContext(size);
    CGRect thumbnailRect  = CGRectZero;
    thumbnailRect.origin = thumnailPoint;
    thumbnailRect.size.width = toWidth;
    thumbnailRect.size.height = toHeight;

    //繪制出圖片(大小已經(jīng)改變)
    [oldImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //結果判斷
    if (newImage == nil) {
        [NSException exceptionWithName:@"提示" reason:@"Error:image scale fail" userInfo:nil];
    }
    UIGraphicsEndImageContext();
    return newImage;
}


-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
    
    UIImage *newImage = nil;
    
    CGSize imageSize = sourceImage.size;
    
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = height / (width / targetWidth);
    CGSize size = CGSizeMake(targetWidth, targetHeight);
    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;
}

圖片的壓縮其實是倆概念,
1、是 “壓” 文件體積變小,但是像素數(shù)不變,長寬尺寸不變,那么質(zhì)量可能下降,
2、是 “縮” 文件的尺寸變小,也就是像素數(shù)減少。長寬尺寸變小,文件體積同樣會減小。
這個 UIImageJPEGRepresentation(image, 0.0),是1的功能。
這個 [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)] 是2的功能。
所以,這倆你得結合使用來滿足需求,不然你一味的用1,導致,圖片模糊的不行,但是尺寸還是很大。

我們還可以對圖片進行部分截取

-(UIImage*)getSubImage:(CGRect)rect
{
    CGImageRefsubImageRef =CGImageCreateWithImageInRect(self.CGImage,rect);
    CGRectsmallBounds =CGRectMake(0,0, CGImageGetWidth(subImageRef),CGImageGetHeight(subImageRef));

    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRefcontext =UIGraphicsGetCurrentContext();
    CGContextDrawImage(context,smallBounds,subImageRef);
    UIImage*smallImage =[UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();

    returnsmallImage;
}

//--------------截取部分圖片到指定位置-------------------------

圖片(UIImage*)img
要截取的起始坐標sx:(int)sx1 sy:(int)sy1
要截取的長度和寬度sw:(int)sw1 sh:(int)sh1
最終要顯示的坐標desx:(int)desx1 desy:(int)desy1

-(UIImage*)objectiveDrawRegion:(UIImage*)img sx:(int)sx1 sy:(int)sy1sw:(int)sw1 sh:(int)sh1 desx:(int)desx1 desy:(int)desy1{
[selfsaveImage:imgname:@"objectiveDrawRegion1.png"];

//創(chuàng)建圖片緩沖
void*imageDataRegion=malloc(screenWidth*screenHeight*32);
CGColorSpaceRefiColorSpaceRegion=CGColorSpaceCreateDeviceRGB();
   CGContextRefiDeviceRegion=CGBitmapContextCreate(imageDataRegion,screenWidth,screenHeight,8,4*screenWidth,iColorSpaceRegion,kCGImageAlphaPremultipliedLast);

//剪切區(qū)域
   CGRectclipRegion=CGRectMake(sx1,sy1,sw1,sh1);
   CGContextClipToRect(iDeviceRegion,clipRegion);

   CGFloatwidthf=img.size.width;
   CGFloatheightf=img.size.height;

CGRect  cg=CGRectMake(0.0,0.0, widthf, heightf);
//畫底圖
   CGContextDrawImage(iDeviceRegion,cg,img.CGImage);

//將緩沖形成圖片
   CGImageRefioffRegion=CGBitmapContextCreateImage(iDeviceRegion);

CGRect  cg1=CGRectMake(desx1,desy1, sw1, sh1);
UIImage *ui=[UIImageimageWithCGImage:ioffRegion];

CGContextDrawImage(當前context,cg1,ui.CGImage);

//清除緩沖
  CGColorSpaceRelease(iColorSpaceRegion);
 CGContextRelease(iDeviceRegion);
  CGImageRelease(ioffRegion);
 free(imageDataRegion);
//    iDeviceRegion=NULL;
//    imageDataRegion=0;

returnui;
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 首先,我們必須明確圖片的壓縮其實是兩個概念: “壓” 是指文件體積變小,但是像素數(shù)不變,長寬尺寸不變,那么質(zhì)量可能...
    李sir35閱讀 11,722評論 1 17
  • 1、確圖片的壓縮的概念: “壓” 是指文件體積變小,但是像素數(shù)不變,長寬尺寸不變,那么質(zhì)量可能下降?!翱s” 是指文...
    HOULI閱讀 19,218評論 7 34
  • 好習慣之先放demo, https://github.com/ssj1314/SSJImage-Scan 好用的話...
    五蘊盛閱讀 1,996評論 6 14
  • 概要: 圖片的兩種壓縮方法1.1 壓縮圖片質(zhì)量1.2 壓縮圖片尺寸壓縮圖片使圖片文件小于指定大小2.1 壓縮圖片質(zhì)...
    leonardni閱讀 66,225評論 40 206
  • 兩種圖片壓縮方法 兩種壓縮圖片的方法:壓縮圖片質(zhì)量(Quality),壓縮圖片尺寸(Size)。 壓縮圖片質(zhì)量 N...
    喵喵嘟嚕啡閱讀 2,107評論 0 9

友情鏈接更多精彩內(nèi)容