How to use UIGraphicsBeginImageContextWithOptions?

I am trying to adapt IT to work with a mesh display. Here's how I found out how to work with retina graphics for UIGraphicsBeginImageContext.

if (UIGraphicsBeginImageContextWithOptions != NULL)
{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0f);
}
else
{
    UIGraphicsBeginImageContext(self.view.bounds.size);
}

However, when I use it, the image looks really huge and does not scale to fit the display. Any ideas how I can tell to resize the captured image to fit the fields? (Please refer to another question to understand what I mean).

+5
source share
2 answers

I wrote this code to accomplish the same thing as described in another post. It works on any iOS device with at least iOS 3.1.

_launcherView - ,

CGFloat scale = 1.0;
if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {        
    CGFloat tmp = [[UIScreen mainScreen]scale];
    if (tmp > 1.5) {
        scale = 2.0;    
    }
} 

if(scale > 1.5) {
    UIGraphicsBeginImageContextWithOptions(_launcherView.frame.size, NO, scale);
} else {
    UIGraphicsBeginImageContext(_launcherView.frame.size);
}

[_launcherView.layer renderInContext:UIGraphicsGetCurrentContext()];

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

CGRect upRect = CGRectMake(0, 0, _launcherView.frame.size.width*scale, (diff - offset)*scale);
CGImageRef imageRefUp = CGImageCreateWithImageInRect([screenshot CGImage], upRect);
[self.screenshot1 setFrame:CGRectMake(0, 0, screenshot1.frame.size.width, diff - offset)];
[screenshot1 setContentMode:UIViewContentModeTop];
UIImage * img1 = [UIImage imageWithCGImage:imageRefUp];
[self.screenshot1 setBackgroundImage:img1 forState:UIControlStateNormal];
CGImageRelease(imageRefUp);

CGRect downRect = CGRectMake(0, (diff - offset)*scale, _launcherView.frame.size.width*scale, (screenshot.size.height - diff + offset)*scale);
CGImageRef imageRefDown = CGImageCreateWithImageInRect([screenshot CGImage], downRect);
[self.screenshot2 setFrame:CGRectMake(0, screenshot1.frame.size.height ,  screenshot2.frame.size.width, _launcherView.frame.size.height - screenshot1.frame.size.height)];
[screenshot2 setContentMode:UIViewContentModeTop];
UIImage * img2 = [UIImage imageWithCGImage:imageRefDown];
[self.screenshot2 setBackgroundImage:img2 forState:UIControlStateNormal];
CGImageRelease(imageRefDown);
+12
- (UIImage *)getNewimage{
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
-1

All Articles