Rah*_*yas 3 iphone cocoa-touch core-graphics objective-c
我有一个视图UIImageView和UIImage设置.如何使用coregraphics使图像清晰或模糊?
Apple有一个名为GLImageProcessing的优秀示例程序,它包含使用OpenGL ES 1.1的非常快速的模糊/锐化效果(意味着它适用于所有iPhone,而不仅仅是3gs).
如果您对OpenGL没有相当的经验,那么代码可能会让您头痛.
沿着OpenGL路线走下去感觉就像疯了一样满足我的需求(模糊图像上的触摸点).相反,我实现了一个简单的模糊过程,它接受一个触点,创建一个包含该触摸点的矩形,对该点中的图像进行采样,然后在源矩形顶部上下颠倒重绘样本图像几次稍微偏移,略微不同的不透明度.这会产生一个相当不错的穷人的模糊效果,而没有疯狂的代码和复杂性.代码如下:
- (UIImage*)imageWithBlurAroundPoint:(CGPoint)point {
CGRect bnds = CGRectZero;
UIImage* copy = nil;
CGContextRef ctxt = nil;
CGImageRef imag = self.CGImage;
CGRect rect = CGRectZero;
CGAffineTransform tran = CGAffineTransformIdentity;
int indx = 0;
rect.size.width = CGImageGetWidth(imag);
rect.size.height = CGImageGetHeight(imag);
bnds = rect;
UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();
// Cut out a sample out the image
CGRect fillRect = CGRectMake(point.x - 10, point.y - 10, 20, 20);
CGImageRef sampleImageRef = CGImageCreateWithImageInRect(self.CGImage, fillRect);
// Flip the image right side up & draw
CGContextSaveGState(ctxt);
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
CGContextConcatCTM(ctxt, tran);
CGContextDrawImage(UIGraphicsGetCurrentContext(), rect, imag);
// Restore the context so that the coordinate system is restored
CGContextRestoreGState(ctxt);
// Cut out a sample image and redraw it over the source rect
// several times, shifting the opacity and the positioning slightly
// to produce a blurred effect
for (indx = 0; indx < 5; indx++) {
CGRect myRect = CGRectOffset(fillRect, 0.5 * indx, 0.5 * indx);
CGContextSetAlpha(ctxt, 0.2 * indx);
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextDrawImage(ctxt, myRect, sampleImageRef);
}
copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return copy;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10042 次 |
| 最近记录: |