在 watchOS 上保存和加载核心图形 UIImage 数组

Save and Load Core Graphic UIImage Array on watchOS

我希望能够保存在带有 watchOS 的 Apple Watch 上创建的 UIImage 数组,并将这一系列图像作为动画作为组背景播放。我可以制作图像阵列并播放它,但我不知道如何 store/save 这些图像,所以我可以 retrieve/load 它们下次我 运行 应用程序时,我不必每次应用 运行s.

构建它们

这是我如何使用 Core Graphics (Swift 3) 构建图像的示例:

import WatchKit
import Foundation


class InterfaceController: WKInterfaceController
{
    @IBOutlet var colourGroup: WKInterfaceGroup!

    override func awake(withContext context: AnyObject?)
    {
        super.awake(withContext: context)

    }

    override func willActivate()
    {
        var imageArray: [UIImage] = []
        for imageNumber in 1...250
        {
            let newImage: UIImage = drawImage(fade: CGFloat(imageNumber)/250.0)
            imageArray.append(newImage)
        }

        let animatedImages = UIImage.animatedImage(with:imageArray, duration: 10)

        colourGroup.setBackgroundImage(animatedImages)
        let imageRange: NSRange = NSRange(location: 0, length: 200)
        colourGroup.startAnimatingWithImages(in: imageRange, duration: 10, repeatCount: 0)

        super.willActivate()
    }

    func drawImage(fade: CGFloat) -> UIImage
    {
        let boxColour: UIColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: fade)

        let opaque: Bool = false
        let scale: CGFloat = 0.0

        let bounds: CGRect = WKInterfaceDevice.current().screenBounds

        let imageSize: CGSize = CGSize(width: bounds.width, height: 20.0)


        UIGraphicsBeginImageContextWithOptions(imageSize, opaque, scale)

        let radius: CGFloat = imageSize.height/2.0

        let rect: CGRect = CGRect(x: 0.0, y: 0.0, width: imageSize.width, height: imageSize.height)

        let selectorBox: UIBezierPath = UIBezierPath(roundedRect: rect, cornerRadius: radius)

        let boxLineWidth: Double = 0.0
        selectorBox.lineWidth = CGFloat(boxLineWidth)
        boxColour.setFill()
        selectorBox.fill()

        // return the image
        let result: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return result

    }

    override func didDeactivate()
    {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

}

基本上,我正在寻找一种方法来保存和加载 [UIImage],我可以使用 UIImage.animatedImage(with:[UIImage], duration: TimeInterval) 和数组

有没有办法保存图像数组,以便我可以在下次 运行 应用程序时加载它而不是重建图像?

谢谢

格雷格

NSKeyedArchiver 和 NSKeyedUnarchiver 成功了。这是 Swift XCode 8b4 的代码:

override func willActivate()
{
    var imageArray: [UIImage] = []

    let fileName: String = "TheRings"
    let fileManager = FileManager.default
    let url = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
    let theURL: URL = url.appendingPathComponent(fileName)!


    if let rings = NSKeyedUnarchiver.unarchiveObject(withFile: theURL.path) as? [UIImage]
    {
        print("retrieving rings - found rings")
        imageArray = rings
    }
    else
    {
        print("retrieving rings - can't find rings, building new ones")

        for imageNumber in 1...250
        {
            let newImage: UIImage = drawImage(fade: CGFloat(imageNumber)/250.0)
            imageArray.append(newImage)
        }
         NSKeyedArchiver.archiveRootObject(imageArray, toFile: theURL.path)
    }

    let animatedImages = UIImage.animatedImage(with:imageArray, duration: 10)

    colourGroup.setBackgroundImage(animatedImages)
    let imageRange: NSRange = NSRange(location: 0, length: 200)
    colourGroup.startAnimatingWithImages(in: imageRange, duration: 10, repeatCount: 0)

    super.willActivate()
}