xcodevectorasset-catalog

Xcode 9 vector images programmatically


Xcode 9 now lets us store PDF vectors in an Asset Catalog, and check the Preserve Vector Data flag to retain the vector information after the compile.

I know you can just set an image with UIImage imageNamed to make use of the vector.

What I'd like to know is how to get at the vector programmatically. For example, if I want a copy of that vector that's 900 points high, how can I get one out of the Asset Catalog.

Ideally it would be something like:

UIImage imageNamed:withHeight:

…but this does not exist.

Is what I want to do impossible right now?


Solution

  • You can add the UIImage to an UIImageView, then set the contentMode to .scaleToFill and finally setting the size of the UIImageView.

    let imageView = UIImageView(frame: CGRect(origin: .zero, size: CGSize(width: 1000, height: 1000)))
    imageView.image = UIImage(named: "circle")
    imageView.contentMode = .scaleToFill
    view.addSubview(imageView)
    

    Does this solve your problem?