如何在iOs中平滑UIImageView的边缘 [英] How to smoothen the edges of a UIImageView in iOs
问题描述
可能重复:
据说,最佳解决方案是在图像周围创建1px透明边框。
UPDATE :这是一个帮助方法,用于向
UIImage
添加透明边框,引自此处: http://vocaro.com/trevor/blog/2009/10/12 / resize-a-uiimage-the-right-way /- (UIImage *)transparentBorderImage:(NSUInteger )borderSize {
//如果图像没有alpha图层,请添加一个
UIImage * image = [self imageWithAlpha];
CGRect newRect = CGRectMake(0,0,image.size.width + borderSize * 2,image.size.height + borderSize * 2);
//构建一个与新大小尺寸相同的上下文
CGContextRef位图= CGBitmapContextCreate(NULL,
newRect.size.width,
newRect.size。高度,
CGImageGetBitsPerComponent(self.CGImage),
0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
//在上下文的中心绘制图像,在边缘周围留下空隙
CGRect imageLocation = CGRectMake(borderSize,borderSize,image.size.width,image.size.height );
CGContextDrawImage(bitmap,imageLocation,self.CGImage);
CGImageRef borderImageRef = CGBitmapContextCreateImage(bitmap);
//创建一个掩码使边框透明,并将其与图像组合
CGImageRef maskImageRef = [self newBorderMask:borderSize size:newRect.size];
CGImageRef transparentBorderImageRef = CGImageCreateWithMask(borderImageRef,maskImageRef);
UIImage * transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef];
//清理
CGContextRelease(位图);
CGImageRelease(borderImageRef);
CGImageRelease(maskImageRef);
CGImageRelease(transparentBorderImageRef);
返回transparentBorderImage;
}
Possible Duplicate:
Any quick and dirty anti-aliasing techniques for a rotated UIImageView?I wanted to change the angle of an UIImageView in my iPhone and app i used the following code:
previewImg.transform = CGAffineTransformMakeRotation(atan2(y2-y1,x2-x1));
and I got it working and the image is below.
In the image, the edges of the image is not smooth. Is there any way to smoothen the edges?
解决方案This has been asked before, check this post here, Any quick and dirty anti-aliasing techniques for a rotated UIImageView?
With that said, the optimal solution would be to create a 1px transparent border around your image.
UPDATE: Here's a helper method to add a transparent border to a
UIImage
, referenced from here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/- (UIImage *)transparentBorderImage:(NSUInteger)borderSize { // If the image does not have an alpha layer, add one UIImage *image = [self imageWithAlpha]; CGRect newRect = CGRectMake(0, 0, image.size.width + borderSize * 2, image.size.height + borderSize * 2); // Build a context that's the same dimensions as the new size CGContextRef bitmap = CGBitmapContextCreate(NULL, newRect.size.width, newRect.size.height, CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage)); // Draw the image in the center of the context, leaving a gap around the edges CGRect imageLocation = CGRectMake(borderSize, borderSize, image.size.width, image.size.height); CGContextDrawImage(bitmap, imageLocation, self.CGImage); CGImageRef borderImageRef = CGBitmapContextCreateImage(bitmap); // Create a mask to make the border transparent, and combine it with the image CGImageRef maskImageRef = [self newBorderMask:borderSize size:newRect.size]; CGImageRef transparentBorderImageRef = CGImageCreateWithMask(borderImageRef, maskImageRef); UIImage *transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef]; // Clean up CGContextRelease(bitmap); CGImageRelease(borderImageRef); CGImageRelease(maskImageRef); CGImageRelease(transparentBorderImageRef); return transparentBorderImage; }
这篇关于如何在iOs中平滑UIImageView的边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!