2012-10-31 24 views
23

मैं UIBezierpath बंद पथ से छवि प्राप्त करना चाहता हूं (छवि देखें)। मैं पर drawRect विधि का उपयोग करके छवि खींचता हूं और रेखाएं drawRect विधि का उपयोग कर खींचती हैं। मैं विशेष बंद पथ छवि कैसे प्राप्त कर सकता हूं? क्रिप्या मेरि सहायता करे। अग्रिम में धन्यवाद।UIBezierPath का उपयोग कर छवि को कैसे फसल करें?

यह कोड बेजियरपाथ खींचने के लिए उपयोग किया जाता है।

UIBezierPath *aPath = [UIBezierPath bezierPath]; 
for (NSString *pointString in pointArray) { 
    if ([pointArray indexOfObject:pointString] == 0) 
     [aPath moveToPoint:CGPointFromString(pointString)]; 
    else 
     [aPath addLineToPoint:CGPointFromString(pointString)]; 
} 
[aPath closePath]; 

enter image description here

+0

हम UIImageView पर एक पंक्ति कैसे आकर्षित कर सकते हैं, जब मैं काम कर रहा है लेकिन UIImageView में काम नहीं कर रहा है कि तुम मुझे मदद कर सकता है UIView पर एक रेखा खींच करने की कोशिश कृपया – dineshprasanna

+0

मनी क्या आपने अपना उद्देश्य प्राप्त किया? – amar

+0

हाय क्या आप अपने प्रश्न का उत्तर दे सकते हैं .... – Rajneesh071

उत्तर

14

आपको लगता है कि के लिए एक shapeLayer उपयोग कर सकते हैं। कुछ की तरह,

UIBezierPath *aPath = [UIBezierPath bezierPath]; 
for (NSString *pointString in pointArray) { 
    if ([pointArray indexOfObject:pointString] == 0) 
     [aPath moveToPoint:CGPointFromString(pointString)]; 
    else 
     [aPath addLineToPoint:CGPointFromString(pointString)]; 
} 
[aPath closePath]; 

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.path = aPath.CGPath; 
[view.layer setMask:shapeLayer];//or make it as [imageview.layer setMask:shapeLayer]; 
//and add imageView as subview of whichever view you want. draw the original image 
//on the imageview in that case 

एक छवि के रूप में यह करने के लिए,

UIGraphicsBeginImageContext(view.bounds.size); 
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

image इसी छवि होनी चाहिए।

+0

धन्यवाद एसीबी ... UIBezierpath से छवि कैसे प्राप्त करें। – Mani

+0

मेरा जवाब अपडेट किया गया। इसे जाँचे। – iDev

+0

धन्यवाद ... मैंने कोशिश की, इसकी छवि और पृष्ठभूमि सफेद रंग है। मैं UIBezierPath भाग की छवि चाहता हूं (केवल कल्पना बिल्ली सिर देखें)। क्या तुम मुझे समझ रहे हो? – Mani

1

आप कोड में सब कुछ ड्राइंग कर रहे हैं, बस का उपयोग करें - addClip, यानी .:

UIBezierPath *aPath = [UIBezierPath bezierPath]; 
for (NSString *pointString in pointArray) { 
    if ([pointArray indexOfObject:pointString] == 0) 
     [aPath moveToPoint:CGPointFromString(pointString)]; 
    else 
     [aPath addLineToPoint:CGPointFromString(pointString)]; 
} 
[aPath closePath]; 


CGContextSaveGState(context); 
{ 
    [aPath addClip]; 
    // draw image 
    [aPath stroke]; 
} 
CGContextRestoreGState(context); 
+0

हाय हाइपरक्रिप्ट ... आपके उत्तर के लिए धन्यवाद। – Mani

+0

विशेष UIBezierPath से छवि कैसे प्राप्त करें। – Mani

+0

एक छवि संदर्भ बनाएं और 'UIGraphicsGetImageFromCurrentImageContext() ' – hypercrypt

3

ठीक है, वहाँ बातें आप नोटिस में ले जाना है कि के एक जोड़े हैं।

  1. आप अपने UIView के बजाय अपने UIImageView गुजर रहे हैं?
  2. यदि आप हैं, तो क्या आपने टच हैंडलिंग के लिए अपनी छवि दृश्य सक्षम की है?
  3. अपने UIImageView को उपclass और drawrect() का उपयोग अपने लिए उस सामान को संभालने के लिए करें।

यदि आप छवि (बिल्ली की तरह) का केवल एक हिस्सा चाहते हैं, तो आपको UIBezierPath के अनुसार अपनी छवि को सबमिट करने की आवश्यकता है।

अपडेट किया गया

निम्नलिखित एक पूरा काम कर उदाहरण है, अपनी आवश्यकताओं के लिए इसे बदल।

ViewController.h:

@interface ViewController : UIViewController 
{ 
    UIBezierPath *aPath; 
} 
@property (nonatomic,retain) NSMutableArray *pathArray; 
@property (nonatomic,retain) NSMutableDictionary *dic; 
@property (nonatomic,retain) IBOutlet UIImageView *imgView; 

@end 

ViewController.m:

@interface ViewController() 
- (IBAction)Crop:(id)sender; 
@end 

@implementation ViewController 
@synthesize pathArray,dic,imgView; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

} 
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView; 
{ 

    NSLog(@"Mask Paths %@",clippingPath); 

    CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
    maskLayer.frame = self.imgView.frame; 
    maskLayer.path = [clippingPath CGPath]; 
    maskLayer.fillColor = [[UIColor whiteColor] CGColor]; 
    maskLayer.backgroundColor = [[UIColor clearColor] CGColor]; 


    self.imgView.layer.mask = maskLayer; 



} 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
    self->aPath = [[UIBezierPath alloc]init]; 
    [aPath moveToPoint:[mytouch locationInView:imgView]]; 

} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
    [aPath addLineToPoint:[mytouch locationInView:imgView 
         ]]; 

} 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 
- (IBAction)Crop:(id)sender 
{ 
    [self setClippingPath:aPath :imgView]; 
} 
+0

हाय, अब हम इस परत को स्पष्ट पृष्ठभूमि के साथ uiimage के रूप में कैसे सहेज सकते हैं? मैंने http://stackoverflow.com/questions/3454356/uiimage-from-calayer-iphone-sdk में उल्लेखित UIImage में CALayer self.imgView.layer.mask को परिवर्तित करने का प्रयास किया। लेकिन यह वांछित परिणाम नहीं है। – ScarletWitch

+0

हाय उमेर सूजन। मैं यूबिज़ीयरपाथ को कैसे समझूं? मैंने प्रलेखन को पढ़ने की कोशिश की लेकिन जैसा कि मैं सिर्फ एक नौसिखिया हूं, मुझे पकड़ पाने में कठिन समय मिल रहा है। कृपया मुझे uibezierpath के कुछ अच्छे ट्यूटोरियल के लिए मार्गदर्शन करें। धन्यवाद। – UsamaMan

+0

थोड़ा देर हो चुकी है, लेकिन आपको अपनी परत को वांछित करना चाहिए जहां वांछित UIImage .. –

0

यह मेरे लिए काम करता है ...

UIBezierPath *path = ....//your path 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef context = CGBitmapContextCreate(nil, frame.size.width, frame.size.height, 8, 4*frame.size.width, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst); 

CGContextAddPath(context, path.CGPath); 
CGContextClip(context); 
CGContextDrawImage(context, frame, image.CGImage); 

[UIImage imageWithCGImage:CGBitmapContextCreateImage(context)]; 
2

उपयोग इस फली ios-helpers

- (UIImage *)imageWithStrokeColor: (UIColor *)stroke_color andFillColor: (UIColor *)fill_color { 

    // adjust bounds to account for extra space needed for lineWidth 
    CGFloat width = self.bounds.size.width + self.lineWidth * 2; 
    CGFloat height = self.bounds.size.height + self.lineWidth * 2; 
    CGRect bounds = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, width, height); 

    // create a view to draw the path in 
    UIView *view = [[UIView alloc] initWithFrame:bounds]; 

    // begin graphics context for drawing 
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]); 

    // configure the view to render in the graphics context 
    [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

    // get reference to the graphics context 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // reverse the y-axis to match the opengl coordinate space 
    CGContextScaleCTM(context, 1, -1); 
    CGContextTranslateCTM(context, 0, -view.bounds.size.height); 

    // translate matrix so that path will be centered in bounds 
    CGContextTranslateCTM(context, -(bounds.origin.x - self.lineWidth), -(bounds.origin.y - self.lineWidth)); 

    // stroke color 
    [stroke_color setStroke]; 
    [self stroke]; 

    //fill color 
    [fill_color setFill]; 
    [self fill]; 

    // get an image of the graphics context 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 

    // end the context 
    UIGraphicsEndImageContext(); 

    return viewImage; 
} 
+1

क्या आप अपने उत्तर में अधिक जानकारी जोड़ सकते हैं? यह अच्छा है कि आपने दस्तावेज़ों और उदाहरण कोड के लिए एक लिंक शामिल किया था, लेकिन क्या आप वर्णन कर सकते हैं कि यह वास्तव में क्या करता है? – bjb568

+0

@ bjb568, क्षमा करें। कोड यहाँ है। – Yijun

+0

बहुत बेहतर। +1 किए हैं। – bjb568

1

स्विफ्ट के लिए यह प्रयास करें: ZImageCropper

ZImageCropper मुख्य हिस्से के रूप निम्नलिखित बातें उपयोग कर रहा है:

  • UIBezierPath
  • UITouch घटनाक्रम
  • CAShapeLayer
  • CoreGraphics
0

iDev के जवाब उत्कृष्ट है, लेकिन आप

UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 1.0); 

का उपयोग करना चाहिए अगर आप को छोड़कर UIGraphicsBeginImageContext(view.bounds.size); पारदर्शी पृष्ठभूमि की जरूरत है, तो डिफ़ॉल्ट छवि संदर्भ द्वारा बनाई गई है opaque == हाँ के साथ, तो आपको इसे NO में बदलना होगा।

0
// This Work 100% 

- (शून्य) touchesBegan: (NSSet *) से छू withEvent: (UIEvent *) घटना {

mouseSwiped = NO; 
UITouch *touch = [touches anyObject]; 
lastPoint = [touch locationInView:self.view]; 
megView = [[UIImageView alloc]initWithFrame:CGRectMake(lastPoint.x , lastPoint.y , k_POINT_WIDTH , k_POINT_WIDTH)]; 
//[megView setImage:[UIImage imageNamed:@"b_image22.png"]]; 
[megView setContentMode:UIViewContentModeScaleToFill]; 
megView.alpha = 2; 
//megView.layer.backgroundColor = [UIColor whiteColor].CGColor; 
//megView.layer.cornerRadius = megView.frame.size.width/2; 
megView.clipsToBounds = YES; 
[self.main_uiview addSubview:megView]; 


self.bezierPath = [UIBezierPath bezierPath]; 
[self.bezierPath moveToPoint:lastPoint]; 

}

// Touchmove method 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

mouseSwiped = YES; 
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 
UIGraphicsBeginImageContext(self.main_uiview.frame.size); 
[self.bg_imageview.image drawInRect:CGRectMake(0, 0, self.main_uiview.frame.size.width, self.main_uiview.frame.size.height)]; 
megView.frame = CGRectMake(currentPoint.x - k_POINT_WIDTH/2 , currentPoint.y - k_POINT_WIDTH/2 , k_POINT_WIDTH , k_POINT_WIDTH); 
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
CGContextStrokePath(UIGraphicsGetCurrentContext()); 


CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red,green ,blue, 0.20); 
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); 
    self.bg_imageview.image = UIGraphicsGetImageFromCurrentImageContext(); 
    [self.bg_imageview setAlpha:opacity]; 

UIGraphicsEndImageContext(); 

lastPoint = currentPoint; 

[self.bezierPath addLineToPoint:lastPoint]; 

}

// TouchEnd Method 


    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

{

[megView removeFromSuperview]; 

}

 // Image Crop Method 


    -(UIImage *)croppedImage 
    { 
UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
[self.bezierPath closePath]; 
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 0.0); 
_b_image = self.bg_imageview.image; 
CGSize imageSize = _b_image.size; 
CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height); 
UIGraphicsBeginImageContextWithOptions(imageSize, NO, [[UIScreen mainScreen] scale]); 
[self.bezierPath addClip]; 
[_b_image drawInRect:imageRect]; 

UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return croppedImage; 

}

संबंधित मुद्दे