NSData contentsOfURL返回零,即使url是有效的swift 2.0

NSData contentsOfURL返回零,即使url是有效的swift 2.0,swift,nsdata,youtube-data-api,Swift,Nsdata,Youtube Data Api,这里还有其他“NSData contentsOfURL returns nil”帖子,但没有一篇能解决我的问题 我正试图通过他们的APIv3从YouTube上获取个人资料图像 //To get the images URL if hover over defaultProfileImageURL it shows it has a significant number of bytes not nil. let defaultProfileImageURL = NSURL(strin

这里还有其他“NSData contentsOfURL returns nil”帖子,但没有一篇能解决我的问题

我正试图通过他们的APIv3从YouTube上获取个人资料图像

//To get the images URL if hover over defaultProfileImageURL it shows it has a significant number of bytes not nil.

    let defaultProfileImageURL =  NSURL(string: "https://i.ytimg.com/i/cRfrPfLqP5kjfEcoNwC7_A/1.jpg")!

        //however defaultProfileImageData is nil even though defaultProfileImageURL has data when converted to NSData. 

    let defaultProfileImageData = NSData(contentsOfURL: defaultProfileImageURL) 

    let defaultProfileImage = UIImage(data: defaultProfileImageData!)

为什么它在不应该的时候却说它是零呢?

可能有很多原因。首先,NSData类上没有此类类方法。NSDictionary在Swift中有此方法,但在NSData中没有。据我所知,swift没有方便的类方法。您需要创建并初始化NSData对象

init(contentsOfURL url: NSURL,
           options readOptionsMask: NSDataReadingOptions) throws

似乎是最好的选择。您可以检查出错的原因。

可能有很多原因。首先,NSData类上没有此类类方法。NSDictionary在Swift中有此方法,但在NSData中没有。据我所知,swift没有方便的类方法。您需要创建并初始化NSData对象

init(contentsOfURL url: NSURL,
           options readOptionsMask: NSDataReadingOptions) throws
似乎是最好的选择。您可以检查出错的原因。

尝试以下操作:

let urlString = "http://herbadmother.com/wp-content/themes/thesis_18/custom/rotator/sample-1.jpg"
let imgURL: NSURL = NSURL(string: urlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            let defaultProfileImageData = NSData(data: data!)

            let defaultProfileImage = UIImage(data: defaultProfileImageData)

            // Do something with your image like: self.imageView.image = defaultProfileImage
        }
        task.resume()
在运行代码之前,将其添加到第一个
(因为苹果在iOS9中的安全措施):

NSAppTransportSecurity
NSExceptionDomains
herbadmother.com
N异常低安全Http负载
NSExceptionMinimumTLSVersion
TLSv1.1
n包括多个域
使用您的域名更改herbadmother.com

试试这个:

let urlString = "http://herbadmother.com/wp-content/themes/thesis_18/custom/rotator/sample-1.jpg"
let imgURL: NSURL = NSURL(string: urlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            let defaultProfileImageData = NSData(data: data!)

            let defaultProfileImage = UIImage(data: defaultProfileImageData)

            // Do something with your image like: self.imageView.image = defaultProfileImage
        }
        task.resume()
在运行代码之前,将其添加到第一个
(因为苹果在iOS9中的安全措施):

NSAppTransportSecurity
NSExceptionDomains
herbadmother.com
N异常低安全Http负载
NSExceptionMinimumTLSVersion
TLSv1.1
n包括多个域

使用您的域名更改herbadmother.com

图像链接不起作用是的。我刚检查了图像链接不起作用是的。我刚检查过,请稍等几秒钟加载数据。请稍等几秒钟加载数据。