2011-05-19 6 views
1

Comment faire pivoter facilement un fichier image enregistré dans le répertoire de documents?Comment faire pivoter le fichier image?

Je l'enregistrer avec le code:

(...) 
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext(); 
NSData* imageData = UIImageJPEGRepresentation(capturedImage, 1.0); 
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
[imageData writeToFile:[docDir stringByAppendingPathComponent:@"saved.jpg"] atomically:NO]; 
UIGraphicsEndImageContext(); 

Répondre

2

facilement accompli en utilisant CGContextRotateCTM():

Trouvé dans une réponse SO ici: How to Rotate a UIImage 90 degrees?

static inline double radians (double degrees) {return degrees * M_PI/180;} 
- (UIImage*) rotateImage:(UIImage*)src rotationDirection:(UIImageOrientation)orientation { 
    UIGraphicsBeginImageContext(src.size); 

    CGContextRef context(UIGraphicsGetCurrentContext()); 
    if (orientation == UIImageOrientationRight) { 
     CGContextRotateCTM (context, radians(90)); 
    } else if (orientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (context, radians(-90)); 
    } else if (orientation == UIImageOrientationDown) { 
     // NOTHING 
    } else if (orientation == UIImageOrientationUp) { 
     CGContextRotateCTM (context, radians(90)); 
    } 

    [src drawAtPoint:CGPointMake(0, 0)]; 

    return UIGraphicsGetImageFromCurrentImageContext(); 
} 
+0

Ce n'est même pas vraiment Obj -C méthode .. J'ai essayé de faire quelque chose, mais il n'a rien fait pivoter dans ce cas. – yosh

+0

C'est une fonction facilement appelable. Mais, si vous préférez une méthode, pas de problème. J'ai changé le code pour refléter cela. – memmons

+0

Désolé, je vérifiais un code de fonction similaire auparavant et j'avais plusieurs erreurs de syntaxe. J'ai déjà essayé CGContextRotateCTM mais je n'ai rien fait, j'essaierai votre solution un peu, merci! – yosh