import UIKit extension UIImage { /// 獲取網路圖片尺寸 /// /// - Parameter url: 網路圖片鏈接 /// - Returns: 圖片尺寸size class func getImageSize(_ url: String?) -> CGSiz ...
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)
}
}