zoukankan      html  css  js  c++  java
  • iOS image.size大小实际输出的值跟图片像素的关系

    test.png (像素 20*20) test@2x.png(像素40*40) test@3x.png(像素 60*60)
    
    UIImage *image = [UIImageimageNamed:@"test.png"];
    
    image.size输出大小为(2020);
    
    UIImage *image = [UIImage imageNamed:@"test@2x.png"];
    
    image.size输出大小为(2020);
    
    UIImage *image = [UIImage imageNamed:@"test@3x.png"];
    
    image.size输出大小为(2020);
    
    image.size输出的大小会自动识别图片是几倍的,如果是3倍的输出的结果就是像素除以3,2倍的像素除以2,image.size在图片处理时会很有用,
    例如下面 CGImageRef,里面的CGRectMake取得是图片像素的大小,如果:代码:
    UIImage *image = [UIImage imageNamed:@"test@2x.png"];
    
    CGImageRef ref = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0,0, image.size.width, image.size.height));
    
    ref 得到的就是test@2x.png这张图片左上角的四分之一大小
    
    正确做法应该是:
    
    
    UIImage *image = [UIImage imageNamed:@"test.png"];
    CGImageRef imageRef = [image CGImage];
    //这个size就是实际图片的像素大小
    CGSize size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
    //这里的rect切割是按图片的像素切割
    CGImageRef ref = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, 0, size.width, size.height));
    //最终获取得到的画布大小
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, 0);
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con, CGRectMake(0, 0, size.width, size.height), ref);
    //    UIImage *ima = [UIImage imageWithCGImage:ref];
    UIImage *ima = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    ————————————————
    版权声明:本文为CSDN博主「tabttoo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/tabttoo/article/details/51258969
     
  • 相关阅读:
    react 和 vue 的优缺点总结
    解决js小数求和出现多位小数问题
    同步循环发请求用promise
    hook中ref使用
    只能输入数字和保留三位的小树
    react添加右键点击事件
    redux
    深拷贝和浅拷贝区别及概念
    pointer-events的css属性。使用该属性可以决定是否能穿透绝对定位元素去触发下面元素的某些行为
    React 使用link在url添加参数(url中不可见)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/14978110.html
Copyright © 2011-2022 走看看