2012-04-09 14 views
25

Ich habe eine Foto-App, wo Sie Aufkleber in einem Abschnitt hinzufügen können. Wenn Sie fertig sind, möchte ich das Bild speichern. Hier ist der Code, den ich machen muss.Wie schneidet man ein Bild in iOS

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(); 

nun das Bild, das ist das Vollbild des Bildes gespeichert wird, was in Ordnung ist, aber jetzt brauche ich das Bild zuzuschneiden und ich weiß nicht, wie. Sie können auf den Link unter dem Bild sehen: http://dl.dropbox.com/u/19130454/Photo%202012-04-09%201%2036%2018%20PM.png

Ich brauche zuzuschneiden: 91px von links und rechts 220px von unten

Jede Hilfe geschätzt sehr würde. Wenn ich die Dinge nicht klar erklärt habe, lassen Sie es mich bitte wissen und ich werde mein Bestes geben, um es zu erklären.

+1

Dies könnte eine gute Quelle für Sie sein: [Open Source Bibliothek zum schnellen Hinzufügen von Bildern in eine iOS-App] (http://maniacdev.com/2011/10/open-source-library-to-add-image-cropping-into-an-ios-app-quickly/) – Lucien

+0

mögliches Duplikat von [Image Cropping API für iOS] (http://stackoverflow.com/questions/7087435/image-crop- ping-api-for-ios) – NAlexN

Antwort

34

Wie sei es so etwas wie dies

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

Vielen Dank für die Hilfe. das hat perfekt funktioniert! – flite3

+3

Dies ignoriert die Bildausrichtung, weil UIImage es unterstützt, aber CGImage nicht – 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

Was ist self.CGImage? – Dvole

+1

Meine Verwendung dieser Methode mein Bild wird korrekt wie erwartet abgeschnitten. Aber es wurde automatisch invertiert. Kannst du mir damit helfen, danke. – iHulk

+0

@ user2534373 versuchen, dies zu entfernen UIGraphicsEndImageContext(); – Mohit

-1

Siehe den Link unten für die Pflanzen Bild

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

Bitte einige Inhalte aus dem Link hinzufügen. – Robert

+0

hi meine selbst ramani eine hilfe .. mein projekt ernte bild wie gesicht form bild auch viele verschiedene typ form ernte und ernte blick bewegen und skalieren für objektive c –

+0

verwenden sie diese demo für bild zuschneiden https://github.com/gavinbunney/Toucan –

2

folgenden Code können Ihnen helfen.

Sie sollten die richtige cropFrame Faust von unten Methode

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

Dann erhalten müssen Sie diese Methode aufrufen beschnittene Bild bekommen

- (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; 
} 
Verwandte Themen