2013-08-13 12 views
8

L'utente può modificare la dimensione del riquadro di ritaglio che è visualizzata come predefinita nella schermata di modifica. Ho provato con il codice seguente:Come ritagliare l'immagine nell'obiettivo c?

- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect { 

    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); 
    UIImage *cropped = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    return cropped; 
} 

Ma ha ritagliato un'area fissa. Come ritagliare l'area selezionata dall'utente?

+1

Prova e passa diversi CGRect al tuo giv it funzionerà funzionerà. Non penso che nessun problema sia nel tuo codice. Problema nelle nozioni di base sulla lingua. Raccomando, per favore leggi alcuni libri o documenti Apple su Objective-C. – Tirth

risposta

6
CGRect clippedRect = CGRectMake(0 ,0,180 ,180); 
    CGImageRef imageRef = CGImageCreateWithImageInRect(imgVw1.image.CGImage, clippedRect); 
    UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    imgVw1Cliped.image=newImage; 

    NSLog(@"%d",imgVw1Cliped.image.imageOrientation); 
+0

Grazie per il vostro aiuto – Anupama

+0

@ user2677875 quando si è soddisfatti della risposta, si prega di aggiornare questa risposta .. bcz ur upvote aiuterà pieno per Mohit .. e questa risposta funziona nel mio progetto. – Bajaj

+0

@Mohit questo codice non sembra funzionare più. Fornisce un errore per ios8. – sunny

22

per Get Ritaglia immagine:

UIImage *croppedImg = nil; 
CGRect cropRect = CGRectMake("AS YOu Need"); //set your rect size. 
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect]; 

Usa seguente codice per la chiamata croppIngimageByImageName: toRect: metodo che tornare UIImage (con dimensioni specifiche dell'immagine)

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect 
    { 
     //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15); 

     CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); 
     UIImage *cropped = [UIImage imageWithCGImage:imageRef]; 
     CGImageRelease(imageRef); 

     return cropped; 
    } 
Problemi correlati