ios - 如何绘制UIImage图像如fit out风格

标签 ios uiimage scale crop

我想从 jpg 文件中获取缩略图并显示为 UIImage。显示样式为fit out模式,如UIViewContentModeScaleAspectFill。下面是示例代码,但是太复杂了。

-(UIImage*)scaleToSize:(CGSize)size   
{  
    CGFloat width = CGImageGetWidth(self.CGImage);  
    CGFloat height = CGImageGetHeight(self.CGImage);    

    NSLog(@"size w=%f, h=%f, image w=%f, h=%f", size.width, size.height, width, height);

    float verticalRadio = size.height*1.0/height;   
    float horizontalRadio = size.width*1.0/width;  

    float radio = 1;  
    if(verticalRadio>1 && horizontalRadio>1)  
    {  
        radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;     
    }  
    else  
    {  
        radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;     
    }  

    width = width*radio;  
    height = height*radio;  
    NSLog(@"width=%f, height=%f", width, height);

    int xPos = (size.width - width)/2;  
    int yPos = (size.height-height)/2;  

    NSLog(@"xpos=%d, ypos=%d", xPos, yPos);

    CGSize sz = CGSizeMake(width, height);  
    UIGraphicsBeginImageContext(sz);    

    //  
    [self drawInRect:CGRectMake(0, 0, width, height)];    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();    

    CGRect rt = CGRectMake(-xPos, -yPos, size.width, size.height);
    UIImage* thub = [scaledImage getSubImage:rt];

    scaledImage = nil;
    return thub;  
} 

    - (UIImage *)getSubImage:(CGRect) rect{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    CGRect smallBounds = CGRectMake(rect.origin.x, rect.origin.y,       CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

    NSLog(@"small bounds x=%f, y=%f, w=%f, h=%f", smallBounds.origin.x, smallBounds.origin.y, smallBounds.size.width, smallBounds.size.height);

    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImg = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();

    return smallImg;
}

最佳答案

我已经更新了方法:

-(UIImage*)getSubImage:(CGSize)size   
{  
    CGFloat width = CGImageGetWidth(self.CGImage);  
    CGFloat height = CGImageGetHeight(self.CGImage);

    float verticalRadio = size.height*1.0/height;   
    float horizontalRadio = size.width*1.0/width;  

    float radio = 1;  
    radio = verticalRadio < horizontalRadio ? horizontalRadio : verticalRadio;

    CGRect displayRect = CGRectMake((size.width - width*radio)/2.0f, 
                                (size.height-height*radio)/2.0f, 
                                width*radio, 
                                height*radio);


    // create a bitmap context and then set the context to current using     
    UIGraphicsBeginImageContext(size);    

    //clip
    UIRectClip(displayRect);

    //draw the bitmap in rect    
    [self drawInRect:displayRect];    

    // from context to get a UIIMage  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();    

    // move context out of buffer
    UIGraphicsEndImageContext();   

    NSLog(@"getSubImage ---->");

    // return. 
    return scaledImage;  
} 

关于ios - 如何绘制UIImage图像如fit out风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185529/

相关文章:

ios - Swift 2.2 中的自定义模式匹配未按预期工作

ios - 更新 : Image is captured but will not display

iphone - 来自 AVCaptureVideoPreviewLayer 的 UIImage

ios - Change NSData Size of a UIImage - Compress And Resize UIImage With a Maximum Megabyte Mb 限制

objective-c - 尝试使用 NSUserDefaults 保存和设置 UISwitch 状态

ios - 检查字符串中是否有违禁单词

ios - 如何在 uiimageview 的区域中添加 alpha

c# - 当 DPI 为 150% 时,我的表单无法正确打印

android - 画一个以捏为中心的圆

css - 如何更改 CSS 以针对全宽正确缩放页面