IBAnimatable / IBFakery

Design and prototype UI by filling UI component in Interface Builder with fake data generator
MIT License
5 stars 0 forks source link

random photos #1

Open phimage opened 7 years ago

phimage commented 7 years ago

Photos must be categorized

The user defined properties could be xxx.image.category

For each categories we must find free-to-use images

Framework could load UIImage or NSImage, compute the name using pattern and random numbers

phimage commented 7 years ago

There is a big issue with photos, how to load images in Interface Builder

An UIImage created in code it's ok

public extension UIImage {
  public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
    let rect = CGRect(origin: .zero, size: size)
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
    color.setFill()
    UIRectFill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    guard let cgImage = image?.cgImage else { return nil }
    self.init(cgImage: cgImage)
  }
}

But using UIImage(named:... , or with Data from bundle

 else if key == "image" {
                if let path = Bundle.main.path(forResource: "first", ofType: "jpg") {
                  let url = URL(fileURLWithPath: path)
                        let data = try! Data(contentsOf: url)

                        let value = UIImage(data: data)
                        view.setValue(value, forKey: viewKey)
                }
            }

EDIT: I think the bundle must be change to something like that let myBundle = Bundle(forClass: self.dynamicType)

some other code to do advanced code

override func prepareForInterfaceBuilder() {
    let processInfo = NSProcessInfo.processInfo()
    let environment = processInfo.environment
    let projectSourceDirectories : AnyObject = environment["IB_PROJECT_SOURCE_DIRECTORIES"]!
    let directories = projectSourceDirectories.componentsSeparatedByString(":")

    if directories.count != 0 {
        let firstPath = directories[0] as String
        let imagePath = firstPath.stringByAppendingPathComponent("PrepareForIBTest/Test.jpg")

        let image = UIImage(contentsOfFile: imagePath)
        self.image = image
    }
}