Image Cropping API for iOS

后端 未结 4 1377
一向
一向 2020-12-05 02:54

Is there any cropping image API for objective C that crops images dynamically in Xcode project? Please provide some tricks or techniques how could I crop camera images in iP

4条回答
  •  心在旅途
    2020-12-05 03:17

    You can use CoreGraphics framework to cropping image dynamically. Here is a example code part of dynamic image crop. I hope this will be helpful for you.

    - (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]]; 
    } 
    

提交回复
热议问题