Guest User

Untitled

a guest
Jan 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. -(IBAction) screenShot: (id) sender{
  2.  
  3. UIGraphicsBeginImageContext(sshot.frame.size);
  4. [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
  5. UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
  6. UIGraphicsEndImageContext();
  7. UIImageWriteToSavedPhotosAlbum(viewImage,nil, nil, nil);
  8.  
  9.  
  10. }
  11.  
  12. UIGraphicsBeginImageContext(CGSize 150,150);
  13.  
  14. //first we will make an UIImage from your view
  15. UIGraphicsBeginImageContext(self.view.bounds.size);
  16. [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
  17. UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
  18. UIGraphicsEndImageContext();
  19.  
  20. //now we will position the image, X/Y away from top left corner to get the portion we want
  21. UIGraphicsBeginImageContext(sshot.frame.size);
  22. [sourceImage drawAtPoint:CGPointMake(-50, -100)];
  23. UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
  24. UIGraphicsEndImageContext();
  25. UIImageWriteToSavedPhotosAlbum(croppedImage,nil, nil, nil);
  26.  
  27. -(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
  28. {
  29. CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
  30. UIImage *img = [UIImage imageWithCGImage:imageRef];
  31. CGImageRelease(imageRef);
  32. return img;
  33. }
  34.  
  35. UIImage *img = [self cropImage:viewImage rect:CGRectMake(150,150,100,100)]; //example
  36.  
  37. UIGraphicsBeginImageContext(sshot.frame.size);
  38. CGContextRef c = UIGraphicsGetCurrentContext();
  39. CGContextTranslateCTM(c, 150, 150); // <-- shift everything up to required position when drawing.
  40. [self.view.layer renderInContext:c];
  41. UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
  42. UIGraphicsEndImageContext();
  43. UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
  44.  
  45. - (UIImage *) croppedPhoto
  46. {
  47. CGFloat ox = self.scrollView.contentOffset.x;
  48. CGFloat oy = self.scrollView.contentOffset.y;
  49. CGFloat zoomScale = self.scrollView.zoomScale;
  50. CGFloat cx = (ox + self.cropRectangleButton.frame.origin.x + 15.0f) * 2.0f / zoomScale;
  51. CGFloat cy = (oy + self.cropRectangleButton.frame.origin.y + 15.0f) * 2.0f / zoomScale;
  52. CGFloat cw = 300.0f / zoomScale;
  53. CGFloat ch = 300.0f / zoomScale;
  54. CGRect cropRect = CGRectMake(cx, cy, cw, ch);
  55.  
  56. NSLog(@"---------- cropRect: %@", NSStringFromCGRect(cropRect));
  57. NSLog(@"--- self.photo.size: %@", NSStringFromCGSize(self.photo.size));
  58.  
  59. CGImageRef imageRef = CGImageCreateWithImageInRect([self.photo CGImage], cropRect);
  60. UIImage *result = [UIImage imageWithCGImage:imageRef];
  61. CGImageRelease(imageRef);
  62.  
  63. NSLog(@"------- result.size: %@", NSStringFromCGSize(result.size));
  64.  
  65. return result;
  66. }
Add Comment
Please, Sign In to add comment