Swift Image 的处理

extension UIImage {
   /* 限定图片的大小 */
    func resize(width:CGFloat, height:CGFloat) -> UIImage {
        let myImageSize = CGSizeMake(width, height)
        UIGraphicsBeginImageContextWithOptions(myImageSize, false, 0.0)
        let myImageRect = CGRectMake(0, 0, myImageSize.width, myImageSize.height)
        self.drawInRect(myImageRect)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
      }

    /*生成纯色图片*/
    class func imageColored(color: UIColor) -> UIImage! {
        let rect = CGRect(x: 0, y: 0, width: 0.5, height: 0.5)
        UIGraphicsBeginImageContextWithOptions(rect.size, CGColorGetAlpha(color.CGColor) == 1, 0)
        let context = UIGraphicsGetCurrentContext()!
        CGContextSetFillColorWithColor(context, color.CGColor)
        CGContextFillRect(context, rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
      }


    /*  压缩图片,最大为1M  */
    func compressImage(maxImageSize: CGFloat, maxSizeWithKB: CGFloat) -> NSData {
        var maxImgWithKB = maxSizeWithKB
        var maxImgSize = maxImageSize

        if maxImgWithKB <= 0 {
            maxImgWithKB = 1024
        }

        if maxImgSize <= 0 {
            maxImgSize = 1024
        }

        // 调整分辨率
        var newSize = CGSizeMake(self.size.width, self.size.height)
        let tempHeight = newSize.height / maxImgSize
        let tempWidth = newSize.width / maxImgSize

        if (tempWidth > 1.0 && tempWidth > tempHeight) {
            newSize = CGSizeMake(self.size.width / tempWidth, self.size.height / tempHeight)
        } else if (tempHeight > 1.0 && tempWidth < tempHeight) {
            newSize = CGSizeMake(self.size.width / tempHeight, self.size.width / tempHeight)
        }

        UIGraphicsBeginImageContext(newSize)
        self.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        // 调整大小
        var imageData = UIImageJPEGRepresentation(newImage!, 1.0)
        var sizeOriginKB = CGFloat(imageData!.length) / 1024

        var resizeRate: CGFloat = 0.9
        while (sizeOriginKB > maxImgWithKB && resizeRate > 1.0) {
            imageData = UIImageJPEGRepresentation(newImage!, resizeRate)
            sizeOriginKB = CGFloat(imageData!.length) / 1024
            resizeRate -= 0.1
        }

        return imageData!
      }


    /*图片着色*/
    func tint(color: UIColor, blendMode: CGBlendMode) -> UIImage {
        let drawRect = CGRectMake(0.0, 0.0, size.width, size.height)
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        color.setFill()
        UIRectFill(drawRect)
        drawInRect(drawRect, blendMode: blendMode, alpha: 1.0)
        let tintedImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return tintedImage
      }

    func tintTheme() -> UIImage {
        return self.tint(UIStyles.getThemeColor(), blendMode: CGBlendMode.DestinationIn)
      }

    func tintGray() -> UIImage {
        return self.tint(UIColor.grayColor(), blendMode: CGBlendMode.DestinationIn)
      }

    func tintLightGray() -> UIImage {
        return self.tint(UIColor.lightGrayColor(), blendMode: CGBlendMode.DestinationIn)
      }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SwiftUI是一种新的UI框架,其中包含了Image视图,使开发者可以方便地加载本地或网络图片。在SwiftUI中,加载网络图片可以使用异步加载程序来避免UI线程的阻塞。 首先,需要创建一个异步加载程序来加载网络图片。可以使用URLSession模块并执行网络请求从URL中加载图片。在URL请求返回后,异步程序可以使用Data类型来保存图片数据,然后将其转换为UIImageCGImage以便于在SwiftUI视图中显示。 为了在SwiftUI中显示网络图片,需要在Image视图中使用URL实例作为图片的来源。当SwiftUI视图构建时,Image会使用异步程序中保存的图片数据来显示网络图片。 以下是使用SwiftUI加载网络图片的示例代码: ``` struct NetworkImage: View { @ObservedObject var imageDownloader: ImageDownloader init(url: URL) { imageDownloader = ImageDownloader(url: url) } var body: some View { Image(uiImage: UIImage(data: imageDownloader.data) ?? UIImage()) .resizable() .aspectRatio(contentMode: .fit) } } class ImageDownloader: ObservableObject { @Published var data = Data() init(url: URL) { URLSession.shared.dataTask(with: url) { (data, _, _) in guard let data = data else { return } DispatchQueue.main.async { self.data = data } }.resume() } } ``` 在上面的代码中,创建了一个名为NetworkImage的视图。该视图包含了一个名为imageDownloader的异步下载程序,用于加载网络图片。在init方法中,imageDownloader使用URL实例来执行异步任务进行图片下载。在异步任务的返回处理中保存数据以更新图片数据。该视图使用Image视图并将UIImage实例转换为Image使用。Image视图的resizable和aspectRatio方法用于设置图片的尺寸和比例。 最后,要注意异步任务可以在SwiftUI视图销毁后继续运行,因此需要确保在异步任务中对视图进行弱引用,避免造成内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值