最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift缩放图片

Swift crifan 1233浏览 0评论

swift image resize

iOS Swift: Resize image to 200x200pt/px – Stack Overflow

iphone – The simplest way to resize an UIImage? – Stack Overflow

iPhone & iPad Application Development Help World: Image Resize in swift ios8

Snip2Code – Resize Image in iOS Swift

代码:

//: Playground – noun: a place where people can play
import UIKit
extension UIImage {
   
    func resize(newSize:CGSize) -> UIImage {
         // here both true and false seems both work to resize
//        let hasAlpha = false
        let hasAlpha = true
        let scale:CGFloat = 0.0 //system will auto get the real factor
       
        UIGraphicsBeginImageContextWithOptions(newSize, !hasAlpha, scale)
       
        self.drawInRect(CGRect(origin: CGPointZero, size: newSize))
        let resizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
       
        UIGraphicsEndImageContext()
        return resizedImage
    }
   
    func resizeToWidth(newWidth:CGFloat) -> UIImage {
        let scale = newWidth / self.size.width
        print("scale=\(scale)")
        let newHeight = self.size.height * scale
        print("newHeight=\(newHeight)")
       
        let newSize = CGSize(width: newWidth, height: newHeight)
       
        return self.resize(newSize)
    }
   
    func resizeToHeight(newHeight:CGFloat) -> UIImage {
        let scale = newHeight / self.size.width
        print("scale=\(scale)")
        let newWidth = self.size.height * scale
        print("newWidth=\(newWidth)")
        let newSize = CGSize(width: newWidth, height: newHeight)
        return self.resize(newSize)
    }
}
let originImage = [#Image(imageLiteral: "mergedGroupHeaderImage_82x82.png")#]
let resized41 = originImage.resize(CGSize(width: 41, height: 41))
let resized30 = originImage.resizeToWidth(30)
let resized50 = originImage.resizeToHeight(60)

效果:

另外,附上之前的代码,共是:

extension UIImage {
//    func resize(scale:CGFloat)-> UIImage {
//        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: size.width*scale, height: size.height*scale)))
//        imageView.contentMode = UIViewContentMode.ScaleAspectFit
//        imageView.image = self
//        UIGraphicsBeginImageContext(imageView.bounds.size)
//        imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
//        let result = UIGraphicsGetImageFromCurrentImageContext()
//        UIGraphicsEndImageContext()
//        return result
//    }
//    func resizeToWidth(width:CGFloat)-> UIImage {
//        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))))
//        imageView.contentMode = UIViewContentMode.ScaleAspectFit
//        imageView.image = self
//        UIGraphicsBeginImageContext(imageView.bounds.size)
//        imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
//        let result = UIGraphicsGetImageFromCurrentImageContext()
//        UIGraphicsEndImageContext()
//        return result
//    }
    func resize(newSize:CGSize) -> UIImage {
        // here both true and false seems both work to resize
        //        let hasAlpha = false
        let hasAlpha = true
        let scale:CGFloat = 0.0 //system will auto get the real factor
       
        UIGraphicsBeginImageContextWithOptions(newSize, !hasAlpha, scale)
       
        self.drawInRect(CGRect(origin: CGPointZero, size: newSize))
       
        let resizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
       
        UIGraphicsEndImageContext()
       
        return resizedImage
    }
   
    func resizeToWidth(newWidth:CGFloat) -> UIImage {
        let scale = newWidth / self.size.width
        print("scale=\(scale)")
        let newHeight = self.size.height * scale
        print("newHeight=\(newHeight)")
       
        let newSize = CGSize(width: newWidth, height: newHeight)
       
        return self.resize(newSize)
    }
   
    func resizeToHeight(newHeight:CGFloat) -> UIImage {
        let scale = newHeight / self.size.width
        print("scale=\(scale)")
        let newWidth = self.size.height * scale
        print("newWidth=\(newWidth)")
       
        let newSize = CGSize(width: newWidth, height: newHeight)
       
        return self.resize(newSize)
    }
   
}

转载请注明:在路上 » [已解决]swift缩放图片

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
20 queries in 0.112 seconds, using 9.37MB memory