【问题标题】:how to draw UIImage image such as fit out style如何绘制 UIImage 图像,例如适合样式
【发布时间】:2012-04-28 10:56:46
【问题描述】:

我想从 jpg 文件中获取缩略图并显示为 UIImage。显示样式为适合模式,例如 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;
}

【问题讨论】:

  • ` float radio = 1; if(verticalRadio>1 && Horizo​​ntalRadio>1) { 无线电 = 垂直无线电 > 水平无线电?水平无线电:垂直无线电; } 其他 { 收音机 = 垂直收音机
  • 这是测试代码,我已经删除了。谢谢
  • 好的,我们会尽力为您提供您正在寻找的答案
  • 我尝试了另一种方式,使用 UIRectClip(displayRect) 方法。会比super方法好。

标签: ios uiimage scale crop


【解决方案1】:

我已经更新了方法:

-(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;  
} 

【讨论】:

  • 这是从 jpg 文件中获取适合样式的缩略图的简单方法。
猜你喜欢
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 2013-01-14
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多