Swift - 根据图片URL获取图片的大小

该方法在swift3以后会报大量的错,博主会在近期针对swift3.0进行更正

在swift3.0中,可以下载下来后再用,方法有多种,殊途同归,都是要先下载的,这里提供两种:

方法1:
        var urlStr = NSURL(string: "http://ww3.sinaimg.cn/mw690/51f76ed7jw1e3ohzmmnffj.jpg")!
        var request = NSURLRequest(url: urlStr as URL)
        var imgData = try! NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: nil)
        var img: UIImage? = nil
        var imageView1 = UIImageView()
        if imgData != nil {
            img = UIImage(data: imgData)!
            imageView1.image = img
            print("width:" + "\(img?.size.width)" + "height:" + "\(img?.size.height)")
        }
方法2:
    let lhhImageView = UIImageView()


        lhhImageView.sd_setImage(with: URL(string: "http://ww3.sinaimg.cn/mw690/51f76ed7jw1e3ohzmmnffj.jpg")) { (image, error, cacheType, imageURL) in
            print("width:" + "\(image?.size.width)" + "height:" + "\(image?.size.height)")
        }


        self.view.addSubview(lhhImageView)

看代码:

  func getImageSize(imageURL:String) ->CGSize {
        var URL:NSURL?
        if imageURL.isKindOfClass(NSString) {
            URL = NSURL(string: imageURL)
        }
        if URL == nil
        {
            return  CGSizeZero             // url不正确
        }
        let request = NSMutableURLRequest(URL: URL!)
        let pathExtendsion = URL?.pathExtension?.lowercaseString

        var size = CGSizeZero
        if pathExtendsion == "png" {
            size = self.getPNGImageSize(request)
        }
        else if pathExtendsion == "gif"
        {
            size = self.getGIFImageSize(request)
        }
        else{
            size = self.getJPGImageSize(request)
        }
        if CGSizeEqualToSize(CGSizeZero, size)                   // 如果获取文件头信息失败,发送异步请求请求原图
        {

            guard let data = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: nil) else{
                return size
            }
            let image = UIImage(data: data)
            if image != nil {
                size = (image?.size)!
            }

        }
        return size

    }
    func getPNGImageSize(request:NSMutableURLRequest) -> CGSize {
        //  获取PNG图片的大小
        request.setValue("bytes=16-23", forHTTPHeaderField: "Range")
        guard let data = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: nil) else{
            return CGSizeZero
        }
        if data.length == 8 {
            var w1:Int = 0
            var w2:Int = 0
            var w3:Int = 0
            var w4:Int = 0
            data.getBytes(&w1, range: NSMakeRange(0, 1))
            data.getBytes(&w2, range: NSMakeRange(1, 1))
            data.getBytes(&w3, range: NSMakeRange(2, 1))
            data.getBytes(&w4, range: NSMakeRange(3, 1))

            let w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4
            var h1:Int = 0
            var h2:Int = 0
            var h3:Int = 0
            var h4:Int = 0
            data.getBytes(&h1, range: NSMakeRange(4, 1))
            data.getBytes(&h2, range: NSMakeRange(5, 1))
            data.getBytes(&h3, range: NSMakeRange(6, 1))
            data.getBytes(&h4, range: NSMakeRange(7, 1))
            let h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4

            return CGSizeMake(CGFloat(w), CGFloat(h));

        }

        return CGSizeZero;

    }

    func getGIFImageSize(request:NSMutableURLRequest) -> CGSize {
        //  获取GIF图片的大小
        request.setValue("bytes=6-9", forHTTPHeaderField: "Range")
        guard var data = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: nil) else{
            return CGSizeZero
        }
        if data.length == 4 {
            var w1:Int = 0
            var w2:Int = 0

            data.getBytes(&w1, range: NSMakeRange(0, 1))
            data.getBytes(&w2, range: NSMakeRange(1, 1))

            var w = w1 + (w2 << 8)
            var h1:Int = 0
            var h2:Int = 0

            data.getBytes(&h1, range: NSMakeRange(2, 1))
            data.getBytes(&h2, range: NSMakeRange(3, 1))
            var h = h1 + (h2 << 8)

            return CGSizeMake(CGFloat(w), CGFloat(h));

        }

        return CGSizeZero;
    }

    func getJPGImageSize(request:NSMutableURLRequest) -> CGSize {
        //  获取JPG图片的大小
        request.setValue("bytes=0-209", forHTTPHeaderField: "Range")
        guard var data = try? NSURLConnection.sendSynchronousRequest(request, returningResponse: nil) else{
            return CGSizeZero
        }
        if data.length <= 0x58 {
            return CGSizeZero

        }
        if data.length < 210 {
            var w1:Int = 0
            var w2:Int = 0

            data.getBytes(&w1, range: NSMakeRange(0x60, 0x1))
            data.getBytes(&w2, range: NSMakeRange(0x61, 0x1))

            var w = (w1 << 8) + w2
            var h1:Int = 0
            var h2:Int = 0

            data.getBytes(&h1, range: NSMakeRange(0x5e, 0x1))
            data.getBytes(&h2, range: NSMakeRange(0x5f, 0x1))
            var h = (h1 << 8) + h2

            return CGSizeMake(CGFloat(w), CGFloat(h));

        }
        else
        {
            var word = 0x0
            data.getBytes(&word, range: NSMakeRange(0x15, 0x1))
            if word == 0xdb {
                data.getBytes(&word, range: NSMakeRange(0x5a, 0x1))
                if word == 0xdb {
                    var w1:Int = 0
                    var w2:Int = 0

                    data.getBytes(&w1, range: NSMakeRange(0xa5, 0x1))
                    data.getBytes(&w2, range: NSMakeRange(0xa6, 0x1))

                    var w = (w1 << 8) + w2
                    var h1:Int = 0
                    var h2:Int = 0

                    data.getBytes(&h1, range: NSMakeRange(0xa3, 0x1))
                    data.getBytes(&h2, range: NSMakeRange(0xa4, 0x1))
                    var h = (h1 << 8) + h2

                    return CGSizeMake(CGFloat(w), CGFloat(h));

                }
                else
                {
                    var w1:Int = 0
                    var w2:Int = 0

                    data.getBytes(&w1, range: NSMakeRange(0x60, 0x1))
                    data.getBytes(&w2, range: NSMakeRange(0x61, 0x1))

                    var w = (w1 << 8) + w2
                    var h1:Int = 0
                    var h2:Int = 0

                    data.getBytes(&h1, range: NSMakeRange(0x5e, 0x1))
                    data.getBytes(&h2, range: NSMakeRange(0x5f, 0x1))
                    var h = (h1 << 8) + h2

                    return CGSizeMake(CGFloat(w), CGFloat(h));
                }
            }
            else {
                return CGSizeZero;
            }
        }

    }

你可能感兴趣的:(Swift开发进行时)