• 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 爬虫的工具列表
    使用rabbitmq手动确认消息的,定时获取队列消息实现
    redis订阅发布简单实现
    ubuntu下打开html页面
    关系数据库基本术语
    事务的基本概念,附图示
    oracle 一对多数据分页查询筛选
    一个在linux环境执行io操作的bug
    再springMVC中自定义文件上传处理解决与原spring中MultipartResolve冲突问题
    oracle存储过程删除树状结构的表数据
  • 原文地址:https://www.cnblogs.com/lijianfan/p/4365497.html
Copyright © 2020-2023  润新知