首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

UIImage压缩跟缩放

2013-07-08 
UIImage压缩和缩放节省大量内存,重绘缩略图;3种方法,分别是调整、居中、填充方式。1、等比例缩放。需要根据图像

UIImage压缩和缩放
节省大量内存,重绘缩略图;

3种方法,分别是调整、居中、填充方式。

1、等比例缩放。需要根据图像的长宽比在其四周留出一些额外区域,用透明像素为图像消关。
+ (CGSize) fitSize: (CGSize)thisSize inSize: (CGSize) aSize
{
    CGFloat scale;
    CGSize newsize = thisSize;
   
    if (newsize.height && (newsize.height > aSize.height))
    {
        scale = aSize.height / newsize.height;
        newsize.width *= scale;
        newsize.height *= scale;
    }
   
    if (newsize.width && (newsize.width >= aSize.width))
    {
        scale = aSize.width / newsize.width;
        newsize.width *= scale;
        newsize.height *= scale;
    }
   
    return newsize;
}
2、抽取部分图像来匹配可用空间。并裁剪掉落在像素区外的任何元素。
//返回调整的缩略图
+ (UIImage *) image: (UIImage *) image fitInSize: (CGSize) viewsize
{
    // calculate the fitted size
    CGSize size = [ImageHelper fitSize:image.size inSize:viewsize];
   
    UIGraphicsBeginImageContext(viewsize);

    float dwidth = (viewsize.width - size.width) / 2.0f;
    float dheight = (viewsize.height - size.height) / 2.0f;
   
    CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height);
    [image drawInRect:rect];
   
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
   
    return newimg; 
}
3、通过匹配可用空间的长度和宽度来填充图像。每个像素都被使用,但是图像将水平或垂直裁剪。
//返回居中的缩略图
+ (UIImage *) image: (UIImage *) image centerInSize: (CGSize) viewsize
{
    CGSize size = image.size;
   
    UIGraphicsBeginImageContext(viewsize);
    float dwidth = (viewsize.width - size.width) / 2.0f;
    float dheight = (viewsize.height - size.height) / 2.0f;
   
    CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height);
    [image drawInRect:rect];
   
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
   
    return newimg; 
}

//返回填充的缩略图
+ (UIImage *) image: (UIImage *) image fillSize: (CGSize) viewsize

{
    CGSize size = image.size;
   
    CGFloat scalex = viewsize.width / size.width;
    CGFloat scaley = viewsize.height / size.height;
    CGFloat scale = MAX(scalex, scaley);   
   
    UIGraphicsBeginImageContext(viewsize);
   
    CGFloat width = size.width * scale;
    CGFloat height = size.height * scale;
   
    float dwidth = ((viewsize.width - width) / 2.0f);
    float dheight = ((viewsize.height - height) / 2.0f);
   
    CGRect rect = CGRectMake(dwidth, dheight, size.width * scale, size.height * scale);
    [image drawInRect:rect];
   
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext(); 
   
    return newimg; 
}

//相对比较简单的压缩
- (UIImage *)image:(UIImage *)image centerInSize:(CGSize)viewsize
{
UIGraphicsBeginImageContext(CGSizeMake(viewsize.width, viewsize.height));
[image drawInRect:CGRectMake(0, 0, viewsize.width, viewsize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
   
return reSizeImage;
}

测试

NSDictionary *cate = [self.cates objectAtIndex:indexPath.row];    UIImage *img1 = [UIImage imageNamed:[[cate objectForKey:@"imageName"] stringByAppendingString:@".jpg"]];    NSData *data1 = UIImageJPEGRepresentation(img1, 1.0);    NSLog(@"data1 = %d",data1.length);    UIImage *img2 = [self image:[UIImage imageNamed:[[cate objectForKey:@"imageName"] stringByAppendingString:@".png"]] centerInSize:CGSizeMake(140, 140)];    NSData *data2 = UIImageJPEGRepresentation(img2, 1.0);    NSLog(@"data2 = %d",data2.length);


打印:
2013-07-01 14:04:03.194 Abviewer_iPhone[8829:907] data1 = 288658
2013-07-01 14:04:03.198 Abviewer_iPhone[8829:907] data2 = 859

热点排行