2012-04-09 5 views
25

में एक छवि कैसे फसल करते हैं मेरे पास एक फोटो ऐप है जहां आप एक सेक्शन में स्टिकर जोड़ सकते हैं। जब आप समाप्त कर लेंगे तो मैं छवि को सहेजना चाहता हूं। यह कोड है जो मुझे करना है।आप आईओएस

if(UIGraphicsBeginImageContextWithOptions != NULL) 
{ 
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 2.5); 
} else { 
    UIGraphicsBeginImageContext(self.view.frame.size); 
} 
CGContextRef contextNew=UIGraphicsGetCurrentContext(); 

[self.view.layer renderInContext:contextNew]; 

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

अब छवि सहेजा जाता है कि छवि है, जो ठीक है की पूर्ण स्क्रीन है, लेकिन अब मैं चित्र काटने के लिए की जरूरत है और मैं कैसे पता नहीं है। आप नीचे दिए गए लिंक पर छवि देख सकते हैं: बाएं से 91px और सही 220px नीचे

किसी भी मदद की बहुत सराहना की जाएगी से: http://dl.dropbox.com/u/19130454/Photo%202012-04-09%201%2036%2018%20PM.png

मैं फसल की जरूरत है। अगर मैंने स्पष्ट रूप से चीजों को समझाया नहीं है, तो कृपया मुझे बताएं और मैं फिर से समझाने के लिए अपनी पूरी कोशिश करूंगा।

+1

यह आपके लिए एक अच्छा स्रोत हो सकता है की जरूरत है एक आईओएस ऐप में छवि क्रॉपिंग को त्वरित रूप से जोड़ने के लिए लाइब्रेरी] (http://maniacdev.com/2011/10/open-source-library-to-add-image-cropping-into-an-ios-app-quickly/) – Lucien

+0

[आईओएस के लिए छवि क्रॉपिंग एपीआई] का संभावित डुप्लिकेट (http://stackoverflow.com/questions/7087435/image-cropping-api-for-ios) – NAlexN

उत्तर

34

यह कैसे

CGRect clippedRect = CGRectMake(self.view.frame.origin.x+91, self.view.frame.origin.y, self.view.frame.size.width-91*2, self.view.frame.size.height-220); 
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect); 
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef); 
+0

मदद के लिए बहुत बहुत धन्यवाद। वह पूरी तरह से काम किया! – flite3

+3

यह छवि अभिविन्यास को अनदेखा करता है क्योंकि UIImage इसका समर्थन करता है लेकिन CGImage – Dustin

1
- (UIImage*)imageByCropping:(CGRect)rect 
{ 
    //create a context to do our clipping in 
    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    //create a rect with the size we want to crop the image to 
    //the X and Y here are zero so we start at the beginning of our 
    //newly created context 
    CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height); 
    CGContextClipToRect(currentContext, clippedRect); 

    //create a rect equivalent to the full size of the image 
    //offset the rect by the X and Y we want to start the crop 
    //from in order to cut off anything before them 
    CGRect drawRect = CGRectMake(rect.origin.x * -1, 
           rect.origin.y * -1, 
           self.size.width, 
           self.size.height); 

    //draw the image to our clipped context using our offset rect 
    // CGContextDrawImage(currentContext, drawRect, self.CGImage); 
    [self drawInRect:drawRect]; // This will fix getting inverted image from context. 

    //pull the image from our cropped context 
    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext(); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    //Note: this is autoreleased 
    return cropped; 
} 
+1

नहीं है self.CGImage क्या है? – Dvole

+1

इस विधि का उपयोग करके मेरी छवि को अपेक्षित रूप से ठीक से फसल की जाती है। लेकिन यह स्वचालित रूप से उलटा हो गया। क्या आप इसे धन्यवाद के साथ मेरी मदद कर सकते हैं। – iHulk

+0

@ user2534373 इस UIGraphicsEndImageContext() को हटाने का प्रयास करें; – Mohit

-1

की तरह कुछ के बारे में चित्र काटने

https://github.com/myang-git/iOS-Image-Crop-View

** How to Use ** 

Very easy! It is created to be a drop-in component, so no static library, no extra dependencies. Just copy ImageCropView.h and ImageCropView.m to your project, and implement ImageCropViewControllerDelegate protocol. 
Use it like UIImagePicker: 
- (void)cropImage:(UIImage *)image{ 
    ImageCropViewController *controller = [[ImageCropViewController alloc] initWithImage:image]; 
    controller.delegate = self; 
    [[self navigationController] pushViewController:controller animated:YES]; 
} 
- (void)ImageCropViewController:(ImageCropViewController *)controller didFinishCroppingImage:(UIImage *)croppedImage{ 
    image = croppedImage; 
    imageView.image = croppedImage; 
    [[self navigationController] popViewControllerAnimated:YES]; 
} 
- (void)ImageCropViewControllerDidCancel:(ImageCropViewController *)controller{ 
    imageView.image = image; 
    [[self navigationController] popViewControllerAnimated:YES]; 
} 
+0

कृपया लिंक से कुछ सामग्री जोड़ें। – Robert

+0

नमस्ते मेरा स्वयं रमनी एक मदद .. मेरी परियोजना फसल छवि जैसे चेहरा आकार छवि भी कई अलग-अलग प्रकार की आकृति फसल और फसल दृश्य चाल और उद्देश्य सी –

+0

के लिए स्केल छवि फसल के लिए इस डेमो का उपयोग करें https://github.com/gavinbunney/Toucan –

2

के लिए नीचे दिए गए लिंक कोड आप मदद कर सकते हैं के बाद का संदर्भ लें। [मुक्त स्रोत:

आप नीचे दिए गए विधि से सही cropFrame मुट्ठी मिलना चाहिए

-(CGRect)cropRectForFrame:(CGRect)frame 
{ 
    // NSAssert(self.contentMode == UIViewContentModeScaleAspectFit, @"content mode must be aspect fit"); 

    CGFloat widthScale = imageview.superview.bounds.size.width/imageview.image.size.width; 
    CGFloat heightScale = imageview.superview.bounds.size.height/imageview.image.size.height; 

    float x, y, w, h, offset; 
    if (widthScale<heightScale) { 
     offset = (imageview.superview.bounds.size.height - (imageview.image.size.height*widthScale))/2; 
     x = frame.origin.x/widthScale; 
     y = (frame.origin.y-offset)/widthScale; 
     w = frame.size.width/widthScale; 
     h = frame.size.height/widthScale; 
    } else { 
     offset = (imageview.superview.bounds.size.width - (imageview.image.size.width*heightScale))/2; 
     x = (frame.origin.x-offset)/heightScale; 
     y = frame.origin.y/heightScale; 
     w = frame.size.width/heightScale; 
     h = frame.size.height/heightScale; 
    } 
    return CGRectMake(x, y, w, h); 
} 

तो फिर तुम इस विधि कॉल करने के लिए फसली छवि प्राप्त करने

- (UIImage *)imageByCropping:(UIImage *)image toRect:(CGRect)rect 
{ 
    // you need to update scaling factor value if deice is not retina display 
    UIGraphicsBeginImageContextWithOptions(rect.size, 
              /* your view opaque */ NO, 
              /* scaling factor */ 2.0); 

    // stick to methods on UIImage so that orientation etc. are automatically 
    // dealt with for us 
    [image drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)]; 

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return result; 
}