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
     
  • 相关阅读:
    【快速选择算法与nth_element函数】【续UVA11300 】
    【贪心+中位数】【UVa 11300】 分金币
    【贪心】【Uva11729】 Commando War
    np.random.randn()、np.random.rand()、np.random.randint()
    numpy.unpackbits()
    tf.get_variable()
    tf.truncated_normal()
    tf.FIFOQueue()
    np.hsplit()
    np.frombuffer()
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/14978110.html
Copyright © 2011-2022 走看看