如何正确地将 UIButton 中的图像放入图像中?
Posted
技术标签:
【中文标题】如何正确地将 UIButton 中的图像放入图像中?【英文标题】:How do I correctly place the image from a UIButton into an Image? 【发布时间】:2012-01-07 22:59:28 【问题描述】:我正在尝试从相机拍摄照片并覆盖 UIButton 中的图像——它已被移动/旋转/缩放到屏幕上的某个位置。
下面的代码正确旋转了图像,但没有将其放置在正确的 X、Y 坐标上。
- (UIImage*)dumpOverlayViewToImage
CGSize imageSize = self.cameraOverlayView.bounds.size;
UIGraphicsBeginImageContext(imageSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, self.button.frame.origin.x, self.button.frame.origin.y);
CGContextConcatCTM(ctx, self.button.transform);
[self.button.imageView.image drawAtPoint:CGPointMake(0,0)];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
我们将不胜感激。
【问题讨论】:
您的视图层次结构是什么样的? self.button 和 self.cameraOverlayView 是同一个超级视图的孩子吗?在调试器控制台中运行po [[(id)UIApp keyWindow] recursiveDescription]
并将输出粘贴到您的问题中。
【参考方案1】:
如果您使用的是配备 Retina 显示屏的设备,您可能需要考虑到这一点。在这种情况下,最简单的方法应该是使用它而不是 UIGraphicsBeginImageContext
:
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
【讨论】:
以上是关于如何正确地将 UIButton 中的图像放入图像中?的主要内容,如果未能解决你的问题,请参考以下文章
关闭 SecondViewController 时,firstViewController 中的 UIButton 无法正确更改图像
如何在 Swift 中 UITest 更改 UIButton 的图像?
如何正确地将静态“正确细节”UITableViewCell 内容与图像对齐?