Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Returns largest possible centered cropped image.
- - (UIImage *)centerCropImage:(UIImage *)image
- {
- // Use smallest side length as crop square length
- CGFloat squareLength = MIN(image.size.width, image.size.height);
- // Center the crop area
- CGRect clippedRect = CGRectMake((image.size.width - squareLength) / 2, (image.size.height - squareLength) / 2, squareLength, squareLength);
- // Crop logic
- CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
- UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- return croppedImage;
- }
- let imageView = UIImageView(image: image)
- imageView.contentMode = .scaleAspectFill
- imageView.clipsToBounds = true
- self.imageView.contentMode = UIViewContentModeScaleAspectFill;
- [self.imageView setClipsToBounds:YES];
- [self.imageView setImage:img];
- - (void)drawMaskLineSegmentTo:(CGPoint)ptTo withMaskWidth:(CGFloat)maskWidth inContext:(NXMaskDrawContext)context{
- if (context == nil)
- return;
- if (context.count <= 0){
- [context addObject:[NSValue valueWithCGPoint:ptTo]];
- return;
- }
- CGPoint ptFrom = [context.lastObject CGPointValue];
- UIGraphicsBeginImageContext(self.maskImage.size);
- [self.maskImage drawInRect:CGRectMake(0, 0, self.maskImage.size.width, self.maskImage.size.height)];
- CGContextRef graphicsContext = UIGraphicsGetCurrentContext();
- CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy);
- CGContextSetRGBStrokeColor(graphicsContext, 1, 1, 1, 1);
- CGContextSetLineWidth(graphicsContext, maskWidth);
- CGContextSetLineCap(graphicsContext, kCGLineCapRound);
- CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y);
- CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y);
- CGContextStrokePath(graphicsContext);
- self.maskImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIGraphicsBeginImageContext(self.displayableMaskImage.size);
- [self.displayableMaskImage drawInRect:CGRectMake(0, 0, self.displayableMaskImage.size.width, self.displayableMaskImage.size.height)];
- graphicsContext = UIGraphicsGetCurrentContext();
- CGContextSetBlendMode(graphicsContext, kCGBlendModeCopy);
- CGContextSetStrokeColorWithColor(graphicsContext, self.displayableMaskColor.CGColor);
- CGContextSetLineWidth(graphicsContext, maskWidth);
- CGContextSetLineCap(graphicsContext, kCGLineCapRound);
- CGContextMoveToPoint(graphicsContext, ptFrom.x, ptFrom.y);
- CGContextAddLineToPoint(graphicsContext, ptTo.x, ptTo.y);
- CGContextStrokePath(graphicsContext);
- self.displayableMaskImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- [context addObject:[NSValue valueWithCGPoint:ptTo]];
- }
Add Comment
Please, Sign In to add comment