可能重复:
Any quick and dirty anti-aliasing techniques for a rotated UIImageView?
我想在iPhone和app中更改UIImageView的角度我使用了以下代码:
previewImg.transform = CGAffineTransformMakeRotation(atan2(y2-y1,x2-x1));
我开始工作,图像在下面。
在图像中,图像的边缘不平滑。有没有办法抚平边缘?
答案 0 :(得分:3)
之前已经询问过,请在此处查看此帖子,Any quick and dirty anti-aliasing techniques for a rotated UIImageView?
据说,最佳解决方案是在图像周围创建1px透明边框。
更新:以下是向UIImage
添加透明边框的辅助方法,参考此处: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;
}