我想在我的 iPhone 和应用程序中更改 UIImageView 的角度,我使用了以下代码:
previewImg.transform = CGAffineTransformMakeRotation(atan2(y2-y1,x2-x1));
我让它工作了,图像在下面。
在图像中,图像的边缘不平滑。有什么方法可以使边缘平滑吗?
我想在我的 iPhone 和应用程序中更改 UIImageView 的角度,我使用了以下代码:
previewImg.transform = CGAffineTransformMakeRotation(atan2(y2-y1,x2-x1));
我让它工作了,图像在下面。
在图像中,图像的边缘不平滑。有什么方法可以使边缘平滑吗?
以前有人问过这个问题,在这里查看这篇文章,旋转 UIImageView 的任何快速而肮脏的抗锯齿技术吗?
话虽如此,最佳解决方案是在图像周围创建一个 1px 的透明边框。
更新:这是一个将透明边框添加到 a 的辅助方法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;
}