0

我对如何UIImageJPEGRepresentation与 Documents 目录中的文件路径相关感到完全困惑。

基本上,当我从文件路径中删除文件,然后调用UIImageJPEGRepresentationaUIImage时,它返回 nil,但如果文件路径没有被删除,则UIImageJPEGRepresentation返回图像的数据表示。

这是我的示例代码来演示我的意思:

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")
        }

    }
}

一次functionA调用,imagePaths_ARRAY是空的,所以内部代码块不执行。UIImages我循环遍历每个图像的数组,然后调用UIImageJPEGRepresentation将其转换为Data要写入文件的对象,然后将其添加imagePathimagePaths_ARRAY数组中以在代码的其他地方使用。

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

但是,当functionA被称为SECOND的时候,imagePaths_ARRAY不是空的,所以我从imagePaths_ARRAY.

但是当再次UIImageJPEGRepresentation调用image时,它返回nil?

我完全不知道为什么,因为每个imageinimages_ARRAY都给了我一个有效的UIImage

[<UIImage: 0x600000085280> size {4288, 2848} orientation 0 scale 1.000000]

如第一句话所述,我想知道如何使用removeItem原因删除文件UIImageJPEGRepresentation以返回 nil?

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

4

1 回答 1

0

似乎您的代码中必须发生其他事情。

我只是试了一下,使用以下代码,我可以反复点击按钮,每次都成功:

//
//  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")
            }

        }

    }

}
于 2017-06-05T12:52:22.280 回答