Swift計(jì)算UIImage尺寸CGSize

import ImageIO

extension UIImage {

? ? /**

? ? *? 根據(jù)圖片url獲取網(wǎng)絡(luò)圖片尺寸

? ? */

? ? class func getSizeWithURL(_ URL: Any?) -> CGSize {

? ? ? ? var url: URL? = nil

? ? ? ? if (URL is URL) {

? ? ? ? ? ? url = URL as? URL

? ? ? ? }

? ? ? ? if (URL is String) {

? ? ? ? ? ? url = URL(string: URL as? String ?? "")

? ? ? ? }

? ? ? ? if URL == nil {

? ? ? ? ? ? return CGSize.zero

? ? ? ? }

? ? ? ? let imageSourceRef = CGImageSourceCreateWithURL(url as? CFURL?, nil)

? ? ? ? var width: CGFloat = 0

? ? ? ? var height: CGFloat = 0

? ? ? ? if imageSourceRef != nil {

? ? ? ? ? ? let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, nil)

? ? ? ? ? ? //以下是對(duì)手機(jī)32位、64位的處理(由網(wǎng)友評(píng)論區(qū)拿到的:小怪獸飼養(yǎng)猿)

? ? ? ? ? ? if imageProperties != nil {

? ? ? ? ? ? ? ? let widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth)

#if __LP64__ && __LP64__

? ? ? ? ? ? ? ? if widthNumberRef != nil {

? ? ? ? ? ? ? ? ? ? CFNumberGetValue(widthNumberRef, CFNumberType.float64Type, &width)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? let heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight)

? ? ? ? ? ? ? ? if heightNumberRef != nil {

? ? ? ? ? ? ? ? ? ? CFNumberGetValue(heightNumberRef, CFNumberType.float64Type, &height)

? ? ? ? ? ? ? ? }

#else

? ? ? ? ? ? ? ? if widthNumberRef != nil {

? ? ? ? ? ? ? ? ? ? CFNumberGetValue(widthNumberRef, CFNumberType.float32Type, &width)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? let heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight)

? ? ? ? ? ? ? ? if heightNumberRef != nil {

? ? ? ? ? ? ? ? ? ? CFNumberGetValue(heightNumberRef, CFNumberType.float32Type, &height)

? ? ? ? ? ? ? ? }

#endif

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return CGSize(width: width, height: height)

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容