Guest User

Untitled

a guest
Dec 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. // Returns largest possible centered cropped image.
  2. - (UIImage *)centerCropImage:(UIImage *)image
  3. {
  4. // Use smallest side length as crop square length
  5. CGFloat squareLength = MIN(image.size.width, image.size.height);
  6. // Center the crop area
  7. CGRect clippedRect = CGRectMake((image.size.width - squareLength) / 2, (image.size.height - squareLength) / 2, squareLength, squareLength);
  8.  
  9. // Crop logic
  10. CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
  11. UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
  12. CGImageRelease(imageRef);
  13. return croppedImage;
  14. }
  15.  
  16. let imageView = UIImageView(image: image)
  17. imageView.contentMode = .scaleAspectFill
  18. imageView.clipsToBounds = true
  19.  
  20. self.imageView.contentMode = UIViewContentModeScaleAspectFill;
  21. [self.imageView setClipsToBounds:YES];
  22. [self.imageView setImage:img];
  23.  
  24. - (void)drawMaskLineSegmentTo:(CGPoint)ptTo withMaskWidth:(CGFloat)maskWidth inContext:(NXMaskDrawContext)context{
  25. if (context == nil)
  26. return;
  27. if (context.count <= 0){
  28. [context addObject:[NSValue valueWithCGPoint:ptTo]];
  29. return;
  30. }
  31. CGPoint ptFrom = [context.lastObject CGPointValue];
  32. UIGraphicsBeginImageContext(self.maskImage.size);
  33. [self.maskImage drawInRect:CGRectMake(0, 0, self.maskImage.size.width, self.maskImage.size.height)];
  34. CGContextRef graphicsContext = UIGraphicsGetCurrentContext();
  35. CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy);
  36. CGContextSetRGBStrokeColor(graphicsContext, 1, 1, 1, 1);
  37. CGContextSetLineWidth(graphicsContext, maskWidth);
  38. CGContextSetLineCap(graphicsContext, kCGLineCapRound);
  39. CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y);
  40. CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y);
  41. CGContextStrokePath(graphicsContext);
  42. self.maskImage = UIGraphicsGetImageFromCurrentImageContext();
  43. UIGraphicsEndImageContext();
  44.  
  45. UIGraphicsBeginImageContext(self.displayableMaskImage.size);
  46. [self.displayableMaskImage drawInRect:CGRectMake(0, 0, self.displayableMaskImage.size.width, self.displayableMaskImage.size.height)];
  47. graphicsContext = UIGraphicsGetCurrentContext();
  48. CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy);
  49. CGContextSetStrokeColorWithColor(graphicsContext, self.displayableMaskColor.CGColor);
  50. CGContextSetLineWidth(graphicsContext, maskWidth);
  51. CGContextSetLineCap(graphicsContext, kCGLineCapRound);
  52. CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y);
  53. CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y);
  54. CGContextStrokePath(graphicsContext);
  55. self.displayableMaskImage = UIGraphicsGetImageFromCurrentImageContext();
  56. UIGraphicsEndImageContext();
  57. [context addObject:[NSValue valueWithCGPoint:ptTo]];
  58. }
Add Comment
Please, Sign In to add comment