pikovayadama
pikovayadama

Reputation: 828

Is there a way to create high-resolution images within my ios apps?

I have an iOS app that generates various images/drawings, which I then save to the device with UIImage class. The images end up being fairly low resolution - is there a way to force a higher resolution, or even generate a vector to be saved from an iOS app?

EDIT: here are some details of how the code runs

To create the image, I create multiple segments - CAShapeLayer()'s with UIBezierPath(). I add those as sublayers to the UIView that has the image created in it, briefly abbreviated so:

for i in 0...segmentCounter-1
{
    let path = UIBezierPath() // this is generated in a function with some funky logic for points, colors, etc - UIBezierPath() is a placeholder
    let shapeLayer  = CAShapeLayer()
    shapeLayer.opacity = 0.8;
    shapeLayer.path = path.CGPath
    self.layer.addSublayer(layer)
}

After that runs a few times and an image with several layers is created, I use the following code to save the image:

    UIGraphicsBeginImageContextWithOptions(self.drawingView.layer.bounds.size, false, 0)

    self.drawingView.drawViewHierarchyInRect(self.drawingView.layer.bounds, afterScreenUpdates: true)
    let img:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIImageWriteToSavedPhotosAlbum(img, self, "image:didFinishSavingWithError:contextInfo:", nil)

Upvotes: 1

Views: 234

Answers (1)

rob mayoff
rob mayoff

Reputation: 385610

If you want to generate a “vector” file, the simplest way is to generate a PDF. You can use UIGraphicsBeginPDFContextToFile or UIGraphicsBeginPDFContextToData instead of UIGraphicsBeginImageContextWithOptions. You can find Apple documentation about using this function (and other functions you'll need to also call) here.

If you want to generate a raster image with more pixels, just pass a larger scale as the last argument to UIGraphicsBeginImageContextWithOptions. The zero you are currently using means “the scale of the primary screen of the current device”, so it's probably either 2 (for a normal Retina screen) or 3 (for an iPhone 6 Plus screen). You can pass any number you want. It doesn't have to be an integer.

Upvotes: 2

Related Questions