iOS開發之圖片壓縮實現

使用下面兩個方法,先按尺寸重繪圖片,而後再下降品質上傳圖片dataspa

#pragma mark 裁剪照片
-(UIImage *)scaleToSize:(UIImage *)image size:(CGSize)size
{
    //建立一個bitmap的context
    //並把他設置成當前的context
    UIGraphicsBeginImageContext(size);
    //繪製圖片的大小
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //從當前context中建立一個改變大小後的圖片
    UIImage *endImage=UIGraphicsGetImageFromCurrentImageContext();
   
    UIGraphicsEndImageContext();
    return endImage;
}
- (void)printImageDataLength:(UIImage *)image
{
    for (int i=1; i<= 10; i++) {
        NSData *data = UIImageJPEGRepresentation(image, i * 0.1);
        NSLog(@"JPEGdata.length%.1lf = %d",i * 0.1,data.length);
    }
    NSData *data = UIImagePNGRepresentation(image);
    NSLog(@"PNGdata.length = %d",data.length);
}

 

UIImage的 + (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation方法
相關文章
相關標籤/搜索