当前位置:   article > 正文

IOS UIGraphicsBeginImageContextWithOptions 版本适配问题_uigraphicsbeginimagecontextwithoptions 适配

uigraphicsbeginimagecontextwithoptions 适配
  1. CGFloat scale = 1.0;
  2. if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
  3. CGFloat tmp = [[UIScreen mainScreen]scale];
  4. if (tmp > 1.5) {
  5. scale = 2.0;
  6. }
  7. }
  8. if(scale > 1.5) {
  9. UIGraphicsBeginImageContextWithOptions(_launcherView.frame.size, NO, scale);
  10. } else {
  11. UIGraphicsBeginImageContext(_launcherView.frame.size);
  12. }
  13. [_launcherView.layer renderInContext:UIGraphicsGetCurrentContext()];
  14. UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
  15. UIGraphicsEndImageContext();
  16. CGRect upRect = CGRectMake(0, 0, _launcherView.frame.size.width*scale, (diff - offset)*scale);
  17. CGImageRef imageRefUp = CGImageCreateWithImageInRect([screenshot CGImage], upRect);
  18. [self.screenshot1 setFrame:CGRectMake(0, 0, screenshot1.frame.size.width, diff - offset)];
  19. [screenshot1 setContentMode:UIViewContentModeTop];
  20. UIImage * img1 = [UIImage imageWithCGImage:imageRefUp];
  21. [self.screenshot1 setBackgroundImage:img1 forState:UIControlStateNormal];
  22. CGImageRelease(imageRefUp);
  23. CGRect downRect = CGRectMake(0, (diff - offset)*scale, _launcherView.frame.size.width*scale, (screenshot.size.height - diff + offset)*scale);
  24. CGImageRef imageRefDown = CGImageCreateWithImageInRect([screenshot CGImage], downRect);
  25. [self.screenshot2 setFrame:CGRectMake(0, screenshot1.frame.size.height , screenshot2.frame.size.width, _launcherView.frame.size.height - screenshot1.frame.size.height)];
  26. [screenshot2 setContentMode:UIViewContentModeTop];
  27. UIImage * img2 = [UIImage imageWithCGImage:imageRefDown];
  28. [self.screenshot2 setBackgroundImage:img2 forState:UIControlStateNormal];
  29. CGImageRelease(imageRefDown);

I've written this code to perform exactly the same thing described in the other post. It works on any iOS device with at least iOS 3.1.

_launcherView is the view that need to be photographed

转帖:http://stackoverflow.com/questions/5146008/how-to-use-uigraphicsbeginimagecontextwithoptions
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号