zoukankan      html  css  js  c++  java
  • 画正方形image

        /** 
         *  剪切图片为正方形 
         * 
         *  @param image   原始图片比如size大小为(400x200)pixels 
         *  @param newSize 正方形的size比如400pixels 
         * 
         *  @return 返回正方形图片(400x400)pixels 
         */  
        - (UIImage *)squareImageFromImage:(UIImage *)image scaledToSize:(CGFloat)newSize {  
            CGAffineTransform scaleTransform;  
            CGPoint origin;  
              
            if (image.size.width > image.size.height) {  
                //image原始高度为200,缩放image的高度为400pixels,所以缩放比率为2  
                CGFloat scaleRatio = newSize / image.size.height;  
                scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);  
                //设置绘制原始图片的画笔坐标为CGPoint(-100, 0)pixels  
                origin = CGPointMake(-(image.size.width - image.size.height) / 2.0f, 0);  
            } else {  
                CGFloat scaleRatio = newSize / image.size.width;  
                scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);  
                  
                origin = CGPointMake(0, -(image.size.height - image.size.width) / 2.0f);  
            }  
              
            CGSize size = CGSizeMake(newSize, newSize);  
            //创建画板为(400x400)pixels  
            if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {  
                UIGraphicsBeginImageContextWithOptions(size, YES, 0);  
            } else {  
                UIGraphicsBeginImageContext(size);  
            }  
              
            CGContextRef context = UIGraphicsGetCurrentContext();  
            //将image原始图片(400x200)pixels缩放为(800x400)pixels  
            CGContextConcatCTM(context, scaleTransform);  
            //origin也会从原始(-100, 0)缩放到(-200, 0)  
            [image drawAtPoint:origin];  
              
            //获取缩放后剪切的image图片  
            image = UIGraphicsGetImageFromCurrentImageContext();  
              
            UIGraphicsEndImageContext();  
              
            return image;  
        }  
  • 相关阅读:
    工作经常使用的SQL整理,实战篇(二)
    工作经常使用的SQL整理,实战篇(一)
    socket编程实例
    C# Socket编程笔记
    SQL Server中的事务与锁
    存储过程学习笔记(SQL数据库
    SQL Server 查询性能优化——创建索引原则(二)
    SQL Server 查询性能优化——创建索引原则(一)(转载)
    PSR-4——新鲜出炉的PHP规范
    PHPUNIT 单元测试
  • 原文地址:https://www.cnblogs.com/lidongq/p/4257274.html
Copyright © 2011-2022 走看看