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

前端 未结 5 581
庸人自扰
庸人自扰 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 01:47

    Couldn't you just create a new graphics context at the desired size, use a CGAffineTransform to scale it down, render the root UIView's root layer, restore the context to the original size and render the image? Haven't tried this for retina content, but this seems to work well for large images that have been scaled down in UIImageViews...

    something like:

    CGSize originalSize = myOriginalImage.size; //or whatever
    
    //create context
    UIGraphicsBeginImageContext(originalSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context); //1 original context
    
    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, originalSize.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    
    //original image
    CGContextDrawImage(context, CGRectMake(0,0,originalSize.width,originalSize.height), myOriginalImage.CGImage);
    
    CGContextRestoreGState(context);//1 restore to original for UIView render;
    
    //scaling
    CGFloat wratio = originalSize.width/self.view.frame.size.width;
    CGFloat hratio = originalSize.height/self.view.frame.size.height;
    
    //scale context to match view size
    CGContextSaveGState(context); //1 pre-scaled size
    CGContextScaleCTM(context, wratio, hratio);
    
    //render 
    [self.view.layer renderInContext:context];
    
    CGContextRestoreGState(context);//1  restore to pre-scaled size;
    
    UIImage *exportImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

提交回复
热议问题