• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何绘制 UIImage 图像,例如适合样式

[复制链接]
菜鸟教程小白 发表于 2022-12-13 13:16:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

-(UIImage*)scaleToSizeCGSize)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 *)getSubImageCGRect) 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;
}



Best Answer-推荐答案


我已经更新了方法:

-(UIImage*)getSubImageCGSize)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 图像,例如适合样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185529/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap