2016-06-29 4 views
1

J'essaie d'implémenter la rotation de l'image dans mon code et d'exécuter quelques tests. Comme je ne connais pas grand-chose au calcul de la rotation, j'ai simplement suivi les instructions que j'avais trouvées sur Internet et mis en œuvre mon code.La rotation de l'image entraîne-t-elle une perte de pixels? (Core Graphics)

J'ai découvert qu'une ligne entière ou une colonne de 1 pixel autour du bord est perdue chaque fois que je fais tourner l'image. (plus que le degré M_PI à la fois)

Cela n'est pas évident lorsque j'héberge l'image en tant qu'objet UIImage, mais vous pouvez le voir lorsque vous enregistrez l'UIImage en tant que fichier.

Voici mon code test de lien: https://github.com/asldkfjwoierjlk/UIImageRotationTest/tree/master

Je ne comprends pas que cette perte se produit. Ai-je manqué quelque chose? Ou est-ce quelque chose mathématiquement inévitable?

Voici la méthode de rotation que j'ai implémentée si vous êtes intéressé.

- (UIImage*)rotateImg:(UIImage*)srcImg 
{ 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0, srcImg.size.width, srcImg.size.height)]; 
CGFloat rotationInDegree = ROTATION_DEGREE; 

CGAffineTransform t = CGAffineTransformMakeRotation(rotationInDegree); 
rotatedViewBox.transform = t; 
CGSize rotatedSize = rotatedViewBox.frame.size; 


// set opaque option to NO to leave the alpha channel intact. 
// Otherwise, there's no point of saving to either png or jpg. 
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 1.0); 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, rotatedSize.width/2.0f, rotatedSize.height/2.0f); 
CGContextRotateCTM(context, rotationInDegree); 
[srcImg drawInRect:CGRectMake(-srcImg.size.width/2.0f, -srcImg.size.height/2.0f, srcImg.size.width, srcImg.size.height)]; 

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


return newImage; 
} 

Répondre

1

vous pouvez gérer l'UIImageView en tant que vue. Ce code fonctionne parfaitement pour moi!

CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
NSNumber *currentAngle = [rotatedViewBox.layer.presentationLayer valueForKeyPath:@"transform.rotation"]; 
rotationAnimation.fromValue = currentAngle; 
rotationAnimation.toValue = @(50*M_PI); 
rotationAnimation.duration = 50.0f;    // this might be too fast 
rotationAnimation.repeatCount = HUGE_VALF;  // HUGE_VALF is defined in math.h so import it 
[rotatedViewBox.layer addAnimation:rotationAnimation forKey:@"rotationAnimationleft"]; 

Bonne codification!