根据图片URL获取图片的尺寸【Swift语言实现】
import UIKit
?
extension UIImage {
?
?
? ? /// 獲取網絡圖片尺寸
? ? ///
? ? /// - Parameter url: 網絡圖片鏈接
? ? /// - Returns: 圖片尺寸size
? ? class func getImageSize(_ url: String?) -> CGSize {
?
? ? ? ? guard let urlStr = url else {
? ? ? ? ? ? return CGSize.zero
? ? ? ? }
? ? ? ? let tempUrl = URL(string: urlStr)
?
? ? ? ? let imageSourceRef = CGImageSourceCreateWithURL(tempUrl! as CFURL, nil)
? ? ? ? var width: CGFloat = 0
? ? ? ? var height: CGFloat = 0
? ? ? ? if let imageSRef = imageSourceRef {
? ? ? ? ? ? let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSRef, 0, nil)
?
? ? ? ? ? ? if let imageP = imageProperties {
? ? ? ? ? ? ? ? let imageDict = imageP as Dictionary
? ? ? ? ? ? ? ? width = imageDict[kCGImagePropertyPixelWidth] as! CGFloat
? ? ? ? ? ? ? ? height = imageDict[kCGImagePropertyPixelHeight] as! CGFloat
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? return CGSize(width: width, height: height)
? ? }
}
?
轉載于:https://www.cnblogs.com/wenxp2006/p/9469366.html
總結
以上是生活随笔為你收集整理的根据图片URL获取图片的尺寸【Swift语言实现】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)函数作用域,匿名函数,函数式编程,
- 下一篇: Quartz框架学习(1)—核心层次结构