Ios 在Swift中删除指定路径的文件时,UIImageJPEGRepresentation返回nil?

Ios 在Swift中删除指定路径的文件时,UIImageJPEGRepresentation返回nil?,ios,swift,uiimagejpegrepresentation,Ios,Swift,Uiimagejpegrepresentation,我完全搞不清楚uiimagejpegresentation与文档目录中文件路径的关系 基本上,当我从文件路径中删除一个文件,然后在UIImage上调用uiimagejpegresentation时,它返回nil,但如果文件路径未被删除,uiimagejpegresentation返回图像的数据表示 下面是我的示例代码,演示我的意思: func functionA() { if imagePaths_ARRAY.isEmpty == false { for path

我完全搞不清楚
uiimagejpegresentation
与文档目录中文件路径的关系

基本上,当我从文件路径中删除一个文件,然后在
UIImage
上调用
uiimagejpegresentation
时,它返回nil,但如果文件路径未被删除,
uiimagejpegresentation
返回图像的数据表示

下面是我的示例代码,演示我的意思:

func functionA()
{
    if imagePaths_ARRAY.isEmpty == false
    {
        for pathToDelete in imagePaths_ARRAY
        {
            if fileManager.fileExists(atPath: pathToDelete)
            {
                do
                {
                    try fileManager.removeItem(atPath: pathToDelete)
                }
                catch let error as NSError
                {
                    print(error.localizedDescription)
                }
            }
            else
            {
                print("ERROR")
            }
        }

        imagePaths_ARRAY.removeAll()
    }

    print(images_ARRAY)

    for image in images_ARRAY
    {
        print(image)

        if let imageData = UIImageJPEGRepresentation(image, 0.5)
        {
            print("RETURN GOOD")
            let imageName = getImageName()

            let imagePath = getDirectoryPath().appending("/" + imageName)

            let success = fileManager.createFile(atPath: imagePath, contents: imageData, attributes: nil)

            if success == true
            {
                imagePaths_ARRAY.append(imagePath)
            }
        }
        else
        {
            print("RETURN NIL")
        }

    }
}
调用非常FIRSTtime
function
imagePaths\u ARRAY
为空,因此内部代码块不会执行。我在一个
UIImages
数组中循环,对于每个图像,我调用
uiimagejpegresentation
将其转换为
Data
对象以写入文件,并将
imagePath
添加到我的
imagePaths\u数组
数组中以在代码的其他地方使用

对于每个图像,我都会看到一个日志
RETURN GOOD
,所有图像都会写入文件

但是,当调用
function
的时间为SECOND时,
imagePaths\u ARRAY
不是空的,因此我从
imagePaths\u ARRAY
中的路径中删除所有文件

但是当再次调用
图像
上的
uiimagejpegresentation
时,它返回nil

我完全搞不懂为什么,因为
images\u数组中的每个
image
都给了我一个有效的
UIImage

[<UIImage: 0x600000085280> size {4288, 2848} orientation 0 scale 1.000000]
[size{42882848}0刻度1.000000]
如第一句所述,我想知道使用
removietem
删除文件如何导致
uiimagejpegresentation
返回nil

更新:当我将
uiimagejpegresentation
替换为
UIImagePNGRepresentation
时,图像被压缩得很好。我完全糊涂了

您的代码中似乎还存在其他问题

我只是尝试了一下,通过下面的代码,我可以反复点击按钮,每次都能成功:

//
//  TestViewController.swift
//

import UIKit

class TestViewController: UIViewController {

    var imagePaths_ARRAY = [String]()
    var images_ARRAY = [UIImage]()

    let fileManager = FileManager.default

    override func viewDidLoad() {
        super.viewDidLoad()

        let names = ["s1", "s2", "s3"]

        for s in names {
            if let img = UIImage(named: s) {
                images_ARRAY.append(img)
            }
        }
    }

    @IBAction func btnTapped(_ sender: Any) {
        functionA()
    }


    func getDirectoryPath() -> String {
        return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    }

    func getImageName() -> String {
        let randomNum:UInt32 = arc4random_uniform(1000000)
        return "randomName_\(randomNum)"
    }

    func functionA()
    {

        print(imagePaths_ARRAY)

        if imagePaths_ARRAY.isEmpty == false
        {
            for pathToDelete in imagePaths_ARRAY
            {
                if fileManager.fileExists(atPath: pathToDelete)
                {
                    do
                    {
                        try fileManager.removeItem(atPath: pathToDelete)
                    }
                    catch let error as NSError
                    {
                        print(error.localizedDescription)
                    }
                }
                else
                {
                    print("ERROR")
                }
            }

            imagePaths_ARRAY.removeAll()
        }

        print(images_ARRAY)

        for image in images_ARRAY
        {
//          print(image)

            if let imageData = UIImageJPEGRepresentation(image, 0.5)
            {
                let imageName = getImageName()

                let imagePath = getDirectoryPath().appending("/" + imageName)

                let success = fileManager.createFile(atPath: imagePath, contents: imageData, attributes: nil)

                if success == true
                {
                    print("RETURN GOOD")
                    imagePaths_ARRAY.append(imagePath)
                }
            }
            else
            {
                print("RETURN NIL")
            }

        }

    }

}

为什么要删除路径?@Optimus:在调用
function
之间,你对
图像数组做了什么?如果是这样,那可能就是问题所在-您的图像数据可能不再是“jpeg表示”的格式。尽量减少调试问题的工作量…@DonMag我不会在调用之间对
images\u ARRAY
做任何其他事情。如果我评论如何检查
imagePaths\u ARRAY.isEmpty==false
,那么
uiimagejpegresentation
的转换没有问题,甚至只是注释掉
试试fileManager.removietem(atPath:pathtodelet)
,效果很好。只是澄清一下,因为我没有机会测试代码,但当你点击一个按钮时,我可以看到代码做的第一件事是删除指向
路径删除
的每个文件,然后循环通过
图像数组
并调用
uiimagejpegresentation
,这与我的代码完全相同,但你说它从不输出
返回NIL
,即,它成功地将图像写入文件?正确。。。它每次都打印“RETURN GOOD”。目前正在查看我的代码,但我很好奇,为什么您认为
uiimagejpegresentation
会返回nil…显然在转换之前,我确实看到一个有效的
UIImage
被传递到函数中,所以我很困惑为什么不能转换
UIImage
。我知道你说过“你的图像数据可能不再是jpeg格式的”。你能详细解释一下你的意思吗?看看苹果的文档,可能有两个原因。不过我想到了一件事——你原来的帖子显示了
size{42882848}
。。。您试图在
图像\u数组中保存多少大小的图像?是否可能内存不足,而图像引用有效,但图像数据实际上不在内存中?我想我必须查看代码的其余部分才能尝试解决它。。。您能否创建一个复制问题的小示例项目?