圖片拉伸處理
實(shí)現(xiàn)類似Android中點(diǎn)9圖的效果瘦棋,拉伸圖片不造成變形且不降低圖片清晰度.
圖片拉伸
主要使用image的
resizableImageWithCapInsets
方法實(shí)現(xiàn)稀火,設(shè)置你想要保留的圖片邊緣大小,保持邊緣不變赌朋,將中間部分進(jìn)行拉伸凰狞。
/**設(shè)置圖片不會(huì)變形拉伸*/
+ (UIImage *)stretchImage:(UIImage *)image edgeInsets:(UIEdgeInsets)insets
{
//CGFloat top; // 頂端蓋高度
//CGFloat bottom; // 底端蓋高度
//CGFloat left; // 左端蓋寬度
//CGFloat right; // 右端蓋寬度
//UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 指定為拉伸模式篇裁,伸縮后重新賦值
image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
return image;
}
截取圖片
截取圖片的一部分
截取圖片
矩形區(qū)域截取
該方法是截取圖片的矩形區(qū)域,給矩形的位置大小就可以了赡若。
+ (UIImage *)cropImage:(UIImage *)image cropRect:(CGRect)rect
{
if(!image){
return nil;
}
CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, rect, subImageRef);
UIImage *cropImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
CGImageRelease(subImageRef);
return cropImage;
}
指定區(qū)域截取
通過一系列的關(guān)鍵點(diǎn)來控制要需要截取的圖片區(qū)域达布,使用path來設(shè)置剪裁區(qū)域。
+ (UIImage *)cropImage:(UIImage *)image pointArray:(NSArray *)pointArr
{
if (pointArr.count == 0 || !image) {
return nil;
}
CGPoint *points = malloc(sizeof(CGPoint) * pointArr.count);
for (int i = 0; i < pointArr.count; ++i) {
points[i] = [[pointArr objectAtIndex:i] CGPointValue];
}
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * image.scale, image.size.height * image.scale));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextAddLines(context, points, pointArr.count);
CGContextClosePath(context);
CGRect boundsRect = CGContextGetPathBoundingBox(context);
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(boundsRect.size);
context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, CGRectMake(0, 0, boundsRect.size.width, boundsRect.size.height));
CGMutablePathRef path = CGPathCreateMutable();
CGAffineTransform transform = CGAffineTransformMakeTranslation(-boundsRect.origin.x, -boundsRect.origin.y);
CGPathAddLines(path, &transform, points, pointArr.count);
CGContextBeginPath(context);
CGContextAddPath(context, path);
CGContextClip(context);
[image drawInRect:CGRectMake(-boundsRect.origin.x, -boundsRect.origin.y, image.size.width * image.scale, image.size.height * image.scale)];
UIImage *cropImage = UIGraphicsGetImageFromCurrentImageContext();
CGPathRelease(path);
UIGraphicsEndImageContext();
return cropImage;
}
給圖片打馬賽克
打碼圖片
馬賽克的主要算法
unsigned char *pixels[4] = {0};
if (i % sizeLevel == 0) {
if (j % sizeLevel == 0) {
memcpy(pixels, bitMapData+4*currentIndex, 4);
}else{
memcpy(bitMapData+4*currentIndex, pixels, 4);
}
}else{
preCurrentIndex = (i-1)*imageW+j;
memcpy(bitMapData+4*currentIndex, bitMapData+4*preCurrentIndex, 4);
}
拖動(dòng)打碼實(shí)現(xiàn)
使用兩張圖片來實(shí)現(xiàn)逾冬,原圖放底部黍聂,馬賽克處理過的圖片在上層,用layer來控制區(qū)域的顯示身腻,具體實(shí)現(xiàn)可查看demo产还。
self.imageLayer = [CALayer layer];
self.imageLayer.frame = self.bounds;
[self.layer addSublayer:self.imageLayer];
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = self.bounds;
self.shapeLayer.lineCap = kCALineCapRound;
self.shapeLayer.lineJoin = kCALineJoinRound;
self.shapeLayer.lineWidth = 20;
self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;
self.shapeLayer.fillColor = nil;//此處必須設(shè)為nil,否則后邊添加addLine的時(shí)候會(huì)自動(dòng)填充
self.imageLayer.mask = self.shapeLayer;
self.path = CGPathCreateMutable();
圖片圓角處理
通過Quartz2D裁剪圖片
/**圓形圖片*/
+ (UIImage *)circleImage:(UIImage *)image
{
if(!image){
return nil;
}
UIGraphicsBeginImageContext(image.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextAddEllipseInRect(ctx, rect);
CGContextClip(ctx);
[image drawInRect:rect];
UIImage *cicleImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cicleImage;
}