zoukankan      html  css  js  c++  java
  • iOS 按frame截取部分图像

    /**
     * 截取部分图像
     *
     **/
    +(UIImage*)getSubImage:(UIImage *)image mCGRect:(CGRect)mCGRect centerBool:(BOOL)centerBool
    {
        
        /*如若centerBool为Yes则是由中心点取mCGRect范围的图片*/
        
            
        float imgwidth = image.size.width;
        float imgheight = image.size.height;
        float viewwidth = mCGRect.size.width;
        float viewheight = mCGRect.size.height;
        CGRect rect;
        if(centerBool)
            rect = CGRectMake((imgwidth-viewwidth)/2, (imgheight-viewheight)/2, viewwidth, viewheight);
        else{
            if (viewheight < viewwidth) {
                if (imgwidth <= imgheight) {
                    rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
                }else {
                    float width = viewwidth*imgheight/viewheight;
                    float x = (imgwidth - width)/2 ;
                    if (x > 0) {
                        rect = CGRectMake(x, 0, width, imgheight);
                    }else {
                        rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
                    }
                }
            }else {
                if (imgwidth <= imgheight) {
                    float height = viewheight*imgwidth/viewwidth;
                    if (height < imgheight) {
                        rect = CGRectMake(0, 0, imgwidth, height);
                    }else {
                        rect = CGRectMake(0, 0, viewwidth*imgheight/viewheight, imgheight);
                    }
                }else {
                    float width = viewwidth*imgheight/viewheight;
                    if (width < imgwidth) {
                        float x = (imgwidth - width)/2 ;
                        rect = CGRectMake(x, 0, width, imgheight);
                    }else {
                        rect = CGRectMake(0, 0, imgwidth, imgheight);
                    }
                }
            }
        }
        
        CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
        CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
        
        UIGraphicsBeginImageContext(smallBounds.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, smallBounds, subImageRef);
        UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
        UIGraphicsEndImageContext();
        
        return smallImage;
    }
     
  • 相关阅读:
    Python容器(zz)
    [Violet]蒲公英 分块
    [JSOI2008]火星人 hash+splay
    [USACO08DEC]拍头Patting Heads 水题
    [国家集训队]整数的lqp拆分 数学推导 打表找规律
    [HNOI2008]水平可见直线 单调栈
    [LNOI2014]LCA 树链剖分 离线 前缀和 思维题
    [SCOI2008]着色方案 递推 记忆化搜索
    [NOI2010]海拔 平面图转对偶图 最小割
    [NOI2008]志愿者招募 网络流 建模
  • 原文地址:https://www.cnblogs.com/lijianfan/p/4365497.html
Copyright © 2011-2022 走看看