zoukankan      html  css  js  c++  java
  • 裁剪并获取固定大小的图片

    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;
    }
     
     
  • 相关阅读:
    国密SM4,javaScript加密 java解密
    使用Nexus搭建Maven私服
    eclipse中使用Maven创建Web项目
    mysql报错码code=exited,status=2的解决方案
    Git出现 fatal: Pathspec 'xxx' is in submodule 'xxx' 异常的解决方案
    php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法
    (转)Git操作
    apt update时出现签名无法验证,公钥失效的解决办法
    提交项目到Github
    分解关联查询
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/3877905.html
Copyright © 2011-2022 走看看