从URL返回图像返回NSURLConnection完成错误 - 代码-1022

时间:2018-04-19 17:56:56

标签: ios swift swift4

我试图解决这个问题: 我知道,有很多文件记载

NSURLConnection finished with error - code -1022

xCode 9 - iOS 11: NSURLConnection - sendAsynchronousRequest fails

嗯,出于某种原因,这不能解决我的问题。

我已经在 info.plist 文件中了:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

所有请求都没问题,但是,当我尝试将string base64解析为图像时,我收到错误:

  

NSURLConnection完成错误 - 代码-1022

这一刻恰好发生了:

let image = datasource[i].File!.base64ToImage()
let imageView = UIImageView(image: image)

当我尝试转换为UIImageView时,会正确显示该消息。

另外,我正在使用此扩展程序:

func base64ToImage() -> UIImage? {
        if let url = URL(string: self),let data = try? Data(contentsOf: url),let image = UIImage(data: data) {
            return image
        }
        return nil
    }

数据源包含所有键的正确信息(也经过测试)。

有人知道如何解决它?

1 个答案:

答案 0 :(得分:0)

var base64String: NSString!
let myImage = UIImage(named:"image.png")
let imageData = UIImageJPEGRepresentation(myImage, 0.9)
base64String = imageData!.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithLineFeed) as NSString!
print(base64String)
Base64String to Data

let thumbnail1Data =  Data(base64Encoded: base64String as String, options: NSData.Base64DecodingOptions()
imageView?.image = UIImage(data: thumbnail1Data as Data)