ios 之 图片压缩 与剪切

有时候为了优化性能,减少内存,提升流畅度,需要对图片进行压缩或者剪切。。。

在保证质量以及清晰度的情况下,对图片处理,对优化性能有很明显的提升。

压缩的图片的步骤很简单:

直接上代码:

//对图片尺寸进行压缩--
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
	// Create a graphics image context
	UIGraphicsBeginImageContext(newSize);
	
	// Tell the old image to draw in this new context, with the desired
	// new size
	[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
	
	// Get the new image from the context
	UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
	
	// End the context
	UIGraphicsEndImageContext();
	
	// Return the new image.
	return newImage;
}

其实就是对图片用drawInRect 重画,不过必须在context中进行。

然后就是对图片进行质量压缩:jpeg或者png

	NSData *imageData = UIImageJPEGRepresentation(imageNew,1.0);
	
	UIImage *image = [UIImage imageWithData:imageData];
上面的是jpeg,转的质量是通过后面的参数控制的:0.0~1.0,质量从低到高。值得一体的是,只会对图片进行一次质量压缩,压缩完之后就不会在压缩了:比如用一个循环对图片压缩质量为0.5,循环10次,压缩仍为0.5。

这两个API如下:

UIKIT_EXTERN NSData *UIImagePNGRepresentation(UIImage *image);                               // return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format

UIKIT_EXTERN NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);  // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)

接下来是对图片进行剪切,其实剪切也是对 图片进行重画,只是对图片有区域的重画而已:

-(UIImage *)cutImageByScalingToSize:(CGSize)size
{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    
    if (size.width > self.size.width) {
        size.width = self.size.width;
    }
    if (size.height > self.size.height) {
        size.height = self.size.height;
    }
    
    CGImageRef cgImg = CGImageCreateWithImageInRect([sourceImage CGImage], CGRectMake( floorf((self.size.width - size.width)/2), floorf((self.size.height - size.height)/2), floorf(size.width), floorf(size.height)));
    newImage = [UIImage imageWithCGImage:cgImg];
    CGImageRelease(cgImg);
    return newImage;
}


待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值