Saving UIView contents in iOS 4 with real size of the images inside (i.e. scale contentes up for save)

前端 未结 5 584
庸人自扰
庸人自扰 2020-12-05 01:34

I have an UIView with many UIImageViews as subviews. The app runs on iOS4 and I use images with retina display resolution (i.e. the images load with scale = 2)

I wa

5条回答
  •  佛祖请我去吃肉
    2020-12-05 02:02

    Import QuartzCore (Click main project, Build Phases, import) and where you need it add:

    #import 
    

    My imageViews are properties, if your are not, ignore the .self and pass the imageViews into the function as parameters, then call renderInContext on the two images in a new UIGraphicsCurrentContext

    - (UIImage *)saveImage
    {
        UIGraphicsBeginImageContextWithOptions(self.mainImage.bounds.size, NO, 0.0);
    
        [self.backgroundImage.layer renderInContext:UIGraphicsGetCurrentContext()];
        [self.mainImage.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage *savedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return savedImage;
    }
    

提交回复
热议问题