【问题标题】:How do I correctly place the image from a UIButton into an Image?如何正确地将 UIButton 中的图像放入图像中?
【发布时间】: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] 并将输出粘贴到您的问题中。

标签: objective-c ios uibutton camera uiimage


【解决方案1】:

如果您使用的是配备 Retina 显示屏的设备,您可能需要考虑到这一点。在这种情况下,最简单的方法应该是使用它而不是 UIGraphicsBeginImageContext

UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多