gpt4 book ai didi

ios - Swift 3.0 将图像写入目录

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:13 29 4
gpt4 key购买 nike

我有一个简单的 ImagePicker 供用户选择或拍摄个人资料照片。我想将此 image 保存到 Home Directory 以便稍后加载。

问题在于未设置图像类型。

    //Save Image
_ = PPimagePicked.image
let imageData = UIImageJPEGRepresentation(PPimagePicked.image!, 0.6)
let compressedJPGImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
// Get Document Root Path
let path = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/profile.jpg")
do {
//Save image to Root
try imageData?.write(to: path, options: .atomic)
print("Saved To Root")
} catch let error {
print(error)
}

确切的错误是:

"[Generic] Creating an image format with an unknown type is an error"

最佳答案

请尝试我在 swift 2.2 中使用的这段代码。以下方法包括 UIImageJPEGRepresentation、UIImagePNGRepresentation

if let image = UIImage(named: "example.png") {
if let data = UIImageJPEGRepresentation(image, 0.8) {
let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
try? data.write(to: filename)
}
}

func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}

if let image = UIImage(named: "example.png") {
if let data = UIImagePNGRepresentation(image) {
let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
try? data.write(to: filename)
}
}

关于ios - Swift 3.0 将图像写入目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39896245/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com