1

I am making a image collage app. After collage the image, I'm trying to draw a output image from ViewController and upload to a site.

UIGraphicsBeginImageContextWithOptions(self.viewDFrame.bounds.size, NO, 0.0f);

[self.viewDFrame.layer renderInContext:UIGraphicsGetCurrentContext()];

 UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

Sometime it can output a completely loaded viewcontroller but mostly it output not completely loaded one like this - with frame only.

This is actual output image and preview image that i wanted to get enter image description here

naotee
  • 61
  • 7

2 Answers2

0

I am using this code to generate single image. Swift 3.0 version

UIGraphicsBeginImageContextWithOptions(CGRect("Frame To get in context"), false, 0.0);
self.view.drawHierarchy(in: CGRect( "Frame Of View" ), afterScreenUpdates: true)

let image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()  

If you have any container view where you add frame and images.

 self.viewContainer.drawHierarchy(in: CGRect( "Frame Of container view" ), afterScreenUpdates: true)

If you want to render image without showing view on screen and frame sizes are fix then refer this question link. How to merge two UIImages?

var bottomImage = UIImage(named: "bottom.png")
var topImage = UIImage(named: "top.png")

var size = CGSize(width: 300, height: 300)
UIGraphicsBeginImageContext(size)

let areaSize = CGRect(x: 0, y: 0, width: size.width, height: size.height)
bottomImage!.drawInRect(areaSize)

topImage!.drawInRect(areaSize, blendMode: kCGBlendModeNormal, alpha: 0.8)

var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Chanchal Warde
  • 983
  • 5
  • 17
  • This not work for me. My view are not displayed on screen, so may be this and my above code only create the view it self, not create the subviews inside. – naotee May 24 '17 at 10:43
  • By the way, if i lower the image quality loaded with PHAsset, the view can be rendered completely – naotee May 24 '17 at 10:45
  • View will be render completely in either cases. But it depends on your need. I was saving that to gallery and i guess you also need to save it in gallery. – Chanchal Warde May 24 '17 at 10:51
  • And what you mean by your view is not displaying on your screen? – Chanchal Warde May 24 '17 at 10:52
  • The view on preview screen and the view i use to render image is different instance, when render image i need to create another with high resolution image. It is use for render the output image only – naotee May 24 '17 at 11:10
  • Is your frames are predefined or user can create free collage?? – Chanchal Warde May 24 '17 at 11:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145011/discussion-between-chanwarde-and-toan-phan). – Chanchal Warde May 24 '17 at 11:12
  • Thanks ChanWarde, may be am not specific with my question, i find out the problem is not from UIGraphicsGetImageFromCurrentImageContext. Check my answer to my own question if you want.. – naotee May 24 '17 at 23:51
0

I finally find out that i set Image using requestImageForAsset, this method is asynchronous so that the the renderInContext not wait for the image to be loaded. I set options.synchronous = YES; for PHImageRequestOptions and the image is loaded before rendering.

naotee
  • 61
  • 7