gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:37:47 25 4
gpt4 key购买 nike

我希望能够保存在带有 watchOS 的 Apple Watch 上创建的 UIImage 数组,并将这一系列图像作为动画作为组背景播放。我可以制作图像数组并播放它,但我不知道如何存储/保存这些图像,以便我可以在下次运行该应用程序时检索/加载它们,这样我就不必在每次运行该应用程序时都构建它们。

这是我如何使用 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 成功了。这是 XCode 8b4 的 Swift 代码:

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

关于swift - 在 watchOS 上保存和加载核心图形 UIImage 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38702183/

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