【问题标题】:Fit image in UIImageView using UIViewContentModeScaleAspectFit使用 UIViewContentModeScaleAspectFit 在 UIImageView 中调整图像
【发布时间】:2016-02-26 18:00:25
【问题描述】:

我在使用 UIImageView 时遇到了一个非常奇怪的问题,我试图将图像(通过截取当前视图的屏幕截图创建)设置为内容模式为 UIViewContentModeScaleAspectFit 的 ImageView。

当我通过 xib 文件中的界面生成器设置图像或设置由 [UIImage imageNamed:] 创建的图像时,它工作正常。他们都可以很好地使用 UIViewContentModeScaleAspectFit。

但是当我拍摄视图的快照并将图像设置为图像视图时,图像不适合 UIImageView。我已经尝试了我在这里找到的所有解决方案,例如 .ClipsToBound = YES 但它们根本不起作用。我现在真的很困惑。

这是我截屏并创建 UIImage 时的代码:

- (UIImage *)screenshotWithRect:(CGRect)captureRect
{
    CGFloat scale = [[UIScreen mainScreen] scale];
    UIImage *screenshot;

    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, scale);
    CGContextClipToRect (UIGraphicsGetCurrentContext(),captureRect);
    {
        if(UIGraphicsGetCurrentContext() == nil)
        {
            NSLog(@"UIGraphicsGetCurrentContext is nil. You may have a UIView (%@) with no really frame (%@)", [self class], NSStringFromCGRect(self.frame));
        }
        else
        {
            [self.layer renderInContext:UIGraphicsGetCurrentContext()];

            screenshot = UIGraphicsGetImageFromCurrentImageContext();
        }
    }
    UIGraphicsEndImageContext();

    return screenshot;
}

当我将图像设置为图像视图时

UIImage* snap = [[UIImage alloc] init];
// start snap shot
UIView* superView = [self.view superview];
CGRect cutRect = [superView convertRect:self.cutView.frame fromView:_viewToCut];
snap = [superView screenshotWithRect:cutRect];
[self.view addSubview:self.editCutFrameView];
// end snap shot -> show edit view
[self.editCutFrameView setImage:snap];

这是比较两个结果的图片:

非常感谢您的帮助。

更新:正如@Saheb Roy 提到的大小,我检查了图像大小,它大约是 400x500 像素,thumbnail.png 的大小是 512x512 像素,所以我认为这与图像的大小无关。

【问题讨论】:

    标签: ios objective-c uiimageview uiimage


    【解决方案1】:

    这是因为在第二种情况下,快照图像本身正是您所看到的大小。因此,图像没有被相应地拉伸或拟合。 较早的图像适合屏幕,因为图像比imageview 大,但与图像的比例不同或相同。

    但是它不适合imageview的那个,图像本身就是这么大,即比imageview的小,因此它不适合边界。

    【讨论】:

    • 您好,感谢您的回答,但我检查了图像并没有那么小,如果它确实是小尺寸图像,那么它应该位于图像视图的中心。但正如你所见,它不在中心。
    • 请回复imageview的大小和图片的大小
    猜你喜欢
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多