简体   繁体   English

使用UIViewContentModeScaleAspectFit在UIImageView中调整图像

[英]Fit image in UIImageView using UIViewContentModeScaleAspectFit

I'm facing a really weird problem with UIImageView, I was trying to set an image - which created by take the screenshot of the current view - to an ImageView with content mode is UIViewContentModeScaleAspectFit. 我面临UIImageView的一个非常奇怪的问题,我试图将图像(通过获取当前视图的屏幕快照创建)设置为具有内容模式的ImageView,即UIViewContentModeScaleAspectFit。

It worked fine when I set the image by the interface builder in the xib file or when I set the image created by [UIImage imageNamed:]. 当我通过界面构建​​器在xib文件中设置图像或设置由[UIImage imageNamed:]创建的图像时,效果很好。 They both worked fine with UIViewContentModeScaleAspectFit. 他们都可以与UIViewContentModeScaleAspectFit一起正常工作。

But when I take the snap shot of a view and set the image to the image view, the image did not fit to the UIImageView. 但是,当我拍摄视图快照并将图像设置为图像视图时,图像不适合UIImageView。 I've tried all the solutions I found on here like .ClipsToBound = YES but they didn't work at all. 我已经尝试过在这里找到的所有解决方案,例如.ClipsToBound = YES,但是它们根本没有用。 I'm really confused by now. 现在我真的很困惑。

Here's the code when I take the screen shot and create the UIImage: 这是我拍摄屏幕快照并创建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;
}

And when I set the image to the image view 当我将图像设置为图像视图时

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];

Here's a picture compare the 2 results: 这是一张比较这两个结果的图片: 在此处输入图片说明

Many thanks for your help. 非常感谢您的帮助。

UPDATE : As @Saheb Roy mentioned about the size, I checked the image size and it's about 400x500px and the thumbnail.png's size is 512x512px so I think it's not about the size of the image. 更新 :正如@Saheb Roy提到的大小一样,我检查了图像大小,它约为400x500px,thumbnail.png的大小为512x512px,所以我认为这与图像的大小无关。

This is because in the second case, the snapshot image is itself exactly that size as you can see . 这是因为在第二种情况下,快照映像本身就是您所看到的大小 Hence the image is not being stretched or fitted accordingly. 因此,图像不会被相应地拉伸或拟合。 Earlier images are fitting to screen accordingly as the images were bigger than the imageview but with different ratio or same than that of the image. 较早的图像适合显示在屏幕上,因为图像大于图像imageview但比率与图像不同或相同。

But the one where it is not fitting to the imageview , the image itself is of that much size, ie smaller than that of the imageview, hence it is NOT being fitted to the bounds. 但是不适合imageview的图像本身具有如此大的尺寸,即小于imageview的尺寸,因此它不适合边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM