• 裁剪并获取固定大小的图片


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
     
    //转换图片大小
    - (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
     
    {
        UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
        [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
        UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
         
        return reSizeImage;
         
    }
     
    //规格化图片并转换为指定大小
    - (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
        double ratio;
        double delta;
        CGPoint offset;
         
        //make a new square size, that is the resized imaged width
        CGSize sz = CGSizeMake(newSize.width, newSize.width);
         
        //figure out if the picture is landscape or portrait, then
        //calculate scale factor and offset
        if (image.size.width > image.size.height) {
            ratio = newSize.width / image.size.width;
            delta = (ratio*image.size.width - ratio*image.size.height);
            offset = CGPointMake(delta/2, 0);
        } else {
            ratio = newSize.width / image.size.height;
            delta = (ratio*image.size.height - ratio*image.size.width);
            offset = CGPointMake(0, delta/2);
        }
         
        //make the final clipping rect based on the calculated values
        CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                     (ratio * image.size.width) + delta,
                                     (ratio * image.size.height) + delta);
         
         
        //start a new context, with scale factor 0.0 so retina displays get
        //high quality image
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
            UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
        } else {
            UIGraphicsBeginImageContext(sz);
        }
        UIRectClip(clipRect);
        [image drawInRect:clipRect];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
         
        return newImage;
    }
     
     
  • 相关阅读:
    pip 使用代理
    npm i -S -D -g 区别
    javascript JSON. 转换 注意事项
    hexo 博客
    kotlin 注意的地方
    VMware-workstation-full-10.0.1-1379776 CN
    分析公司shareaholic报告:Chrome浏览器使用量居首
    搜狗高速浏览器4.2正式版发布
    中行用户购买KIS2014 68元/3年,时间:2013.10.18-2013.11.18
    1元抢卡巴KAV_不限量疯抢即日起至2013.10.31截止
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/3877905.html
Copyright © 2020-2023  润新知