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;
    }
     
     
  • 相关阅读:
    连接Excel文件时,未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
    C# 中LinkLabel的简单使用
    体验安装金蝶K/3 Wise 13.0(图像)
    VS2008让自己掌控的定义编译项目后,自己主动添加到工具箱
    哥哥牟:由于道路的明星莫属,它救了我的女儿!
    【SSH之旅】一步学习的步Struts1相框(三):分析控制Struts1示例
    Ubuntu14.04 工作区设置
    Android定调的发展
    Spark1.0.0 学习路径
    oracle 11g 基于磁盘的备份rman duplicate
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/3877905.html
Copyright © 2011-2022 走看看