2016-08-01 47 views
0

我希望能夠保存在Apple Watch上用watchOS創建的UIImage陣列,並將該系列圖像作爲動畫作爲組背景進行播放。我可以製作圖像數組並播放它,但我無法弄清楚如何存儲/保存這些圖像,以便在下一次運行應用程序時檢索/加載它們,這樣我就不必在每次運行應用程序時都構建它們。在watchOS上保存並加載Core圖形UIImage Array

這裏是我怎樣建立與核心圖形(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(與:[UIImage],持續時間:TimeInterval)與陣列

有沒有辦法保存圖像數組,所以我可以加載它下次運行應用程序,而不是重建圖像?

感謝

格雷格

回答

0

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