2016-06-29 10 views
1

Ich versuche die Bildrotation in meinem Code zu implementieren und einige Tests durchzuführen. Da ich nicht viel darüber weiß, wie die Rotation ausgeführt wird, folgte ich nur einigen Anweisungen, die ich im Internet gefunden hatte, und implementierte meinen Code.Führt die Bildrotation zum Verlust von Pixeln? (Core Graphics)

Ich fand heraus, dass eine ganze Zeile oder Spalte von 1 Pixel um den Rand jedes Mal verloren geht, wenn ich das Bild rotiere. (mehr als M_PI Grad zu einem Zeitpunkt)

Dies ist nicht offensichtlich, wenn ich das Bild als UIImage-Objekt hosten, aber Sie können es sehen, wenn Sie das UIImage als Datei speichern.

Hier ist mein Test-Code-Link: https://github.com/asldkfjwoierjlk/UIImageRotationTest/tree/master

Ich verstehe nicht, dass dieser Verlust geschieht. Habe ich etwas verpasst? Oder ist es mathematisch unvermeidbar?

Hier ist die Rotationsmethode, die ich implementiert habe, wenn Sie interessiert sind.

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

Antwort

1

können Sie das UIImageView als Ansicht behandeln. Dieser Code funktioniert perfekt für mich!

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"]; 

Glückliche Kodierung!