美文网首页
IOS 根据显示的空间尺寸来压缩显示图片尺寸

IOS 根据显示的空间尺寸来压缩显示图片尺寸

作者: 贼噶人 | 来源:发表于2019-06-25 14:12 被阅读0次
        //按照显示控件的size来设置重新加载图片的尺寸
        func compresImage(srcImage:UIImage,size:CGSize)->UIImage{
            if size.width >= srcImage.size.width
                && size.height >= srcImage.size.height {
                return srcImage
            }
            var newSize = CGSize.init()
            if srcImage.size.width > srcImage.size.height {
                if size.width < srcImage.size.width {
                    newSize.height = size.width * srcImage.size.height / srcImage.size.width;
                    newSize.width = size.width;
                }
            } else {
                if size.height < srcImage.size.height {
                    newSize.width = size.height * srcImage.size.width / srcImage.size.height;
                    newSize.height = size.height;
                }
            }
            UIGraphicsBeginImageContext(newSize)
            srcImage.draw(in: CGRect.init(origin: CGPoint.init(x: 0, y: 0)
                    , size: newSize))
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage!;
        }
    

    相关文章

      网友评论

          本文标题:IOS 根据显示的空间尺寸来压缩显示图片尺寸

          本文链接:https://www.haomeiwen.com/subject/rcvtcctx.html