2010-04-16 16 views
3

Je développe la simple UIApplication dans laquelle je veux recadrer l'UIImage (au format .jpg) à l'aide de CGContext. Le code développé jusqu'à maintenant comme suit,Problème lors du recadrage de UIImage à l'aide de CGContext?

CGImageRef graphicOriginalImage = [originalImage.image CGImage];

UIGraphicsBeginImageContext(originalImage.image.size); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGBitmapContextCreateImage(graphicOriginalImage); 

CGFloat fltW = originalImage.image.size.width; 
CGFloat fltH = originalImage.image.size.height; 
CGFloat X = round(fltW/4); 
CGFloat Y =round(fltH/4); 
CGFloat width = round(X + (fltW/2)); 
CGFloat height = round(Y + (fltH/2)); 

CGContextTranslateCTM(ctx, 0, image.size.height); 
CGContextScaleCTM(ctx, 1.0, -1.0); 
CGRect rect = CGRectMake(X,Y ,width ,height); 
CGContextDrawImage(ctx, rect, graphicOriginalImage); 

croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 

return croppedImage; 

} Le code ci-dessus est a bien fonctionné, mais il ne peut pas rogner l'image. La mémoire d'image originale et la mémoire d'image recadrée seront identiques (égales à la mémoire d'image originale). Le code ci-dessus est bon pour recadrer l'image ??????????????????

Répondre

1

Le contexte que vous créez pour dessiner l'image a la même taille que l'image d'origine. C'est pourquoi ils ont la même taille.

Si vous ne souhaitez pas réinventer la roue, jetez un coup d'œil au projet TouchCode sur Google Code. Vous trouverez les catégories UIImage qui font le travail (voir UIImage_ThumbnailExtensions.m).

+0

@Laurent Etiemble, Ce code est-il téléchargeable? – Tirth

+0

Pour l'instant, il n'y a pas de téléchargement disponible. Mais vous pouvez utiliser Mercurial pour cloner le dépôt. –

+0

Avez-vous une idée sur le recadrage de l'image en utilisant le pointeur en pixels d'anf en utilisant les méthodes CGContext? – Tirth

14

Voici une bonne façon de recadrer une image à un CGRect:


- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(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, 
           imageToCrop.size.width, 
           imageToCrop.size.height); 

    //draw the image to our clipped context using our offset rect 
    CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage); 

    //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; 
} 
 

Ou une autre façon:


- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
 { 
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
 
    UIImage *cropped = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 


 return cropped;
 
} 

De http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/.