如何将图像缩放到帧?

Posted

技术标签:

【中文标题】如何将图像缩放到帧?【英文标题】:How to scale an image to a frame? 【发布时间】:2011-11-11 23:32:40 【问题描述】:

是否可以将UIImage 缩放到框架的大小?拍摄一张大图,然后将其“强行放入”较小的方框中。

请举例说明

【问题讨论】:

【参考方案1】:

如果你实际上是在谈论改变 UIImage 而不仅仅是改变显示框架,这可以按照 Mark 的描述来完成,通过改变 UIImageView 的 UIViewContentMode,你可以用下面的 UIImage 改变实际图像的大小扩展名(取自source)

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize

    UIImage *sourceImage = self;
    UIImage *newImage = nil;        
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
    
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) 
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor)
        
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        
        else 
            if (widthFactor < heightFactor)
            
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            
           

    UIGraphicsBeginImageContext(targetSize); // this will crop

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil) 
        NSLog(@"could not scale image");

    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;

【讨论】:

【参考方案2】:

这个问题的可能骗局: How to scale a UIImageView proportionally?

我也会复制接受的答案文本。假设你的框架是一个 uiview:

imageView.contentMode = UIViewContentModeScaleAspectFit; 会做你想做的。

来自文档,这里是其他缩放选项

UIViewContentMode

Specifies how a view adjusts its content when its size changes.
typedef enum 
   UIViewContentModeScaleToFill,
   UIViewContentModeScaleAspectFit,
   UIViewContentModeScaleAspectFill,
   UIViewContentModeRedraw,
   UIViewContentModeCenter,
   UIViewContentModeTop,
   UIViewContentModeBottom,
   UIViewContentModeLeft,
   UIViewContentModeRight,
   UIViewContentModeTopLeft,
   UIViewContentModeTopRight,
   UIViewContentModeBottomLeft,
   UIViewContentModeBottomRight,
 UIViewContentMode;

苹果文档在这里: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html

【讨论】:

他谈到了 UIImage——这不会缩放图像,而只会缩放视图所绘制的内容

以上是关于如何将图像缩放到帧?的主要内容,如果未能解决你的问题,请参考以下文章

如何限制将捏缩放图像移动到图像边框?

如何将图像 UIButton 缩放到插图?

如何在悬停时将过渡速度应用于缩放图像?

如何将图像居中和自动缩放?

在opencv中,如何将灰度图像复制并缩放为另一个彩色图像

如何缩放并排放置的两个图像?