最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift缩放图片

Swift crifan 2177浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.212 seconds, using 22.13MB memory