Tworzenie interfejsu użytkownika z obróconego widoku interfejsu użytkownika
Mam UIImageView z obrazem w nim. Obróciłem obraz przed wyświetlaniem, ustawiając właściwość transform UIImageView na Cgaffinetransformmakerotation (angle), gdzie kąt jest kątem w radianach.
Chcę być w stanie utworzyć inny interfejs, który odpowiada wersji obróconej, którą mogę zobaczyć w moim widoku.
Jestem prawie na miejscu, obracając kontekst obrazu otrzymuję obrócony obraz:
- (UIImage *) rotatedImageFromImageView: (UIImageView *) imageView
{
UIImage *rotatedImage;
// Get image width, height of the bounding rectangle
CGRect boundingRect = [self getBoundingRectAfterRotation: imageView.bounds byAngle:angle];
// Create a graphics context the size of the bounding rectangle
UIGraphicsBeginImageContext(boundingRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Rotate and translate the context
CGAffineTransform ourTransform = CGAffineTransformIdentity;
ourTransform = CGAffineTransformConcat(ourTransform, CGAffineTransformMakeRotation(angle));
CGContextConcatCTM(context, ourTransform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);
// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
// Clean up
UIGraphicsEndImageContext();
return rotatedImage;
}
Jednak obraz nie jest obracany wokół jego środka. Próbowałem wszystkich rodzajów przekształceń połączonych z moim obrotem, aby obrócić go wokół centrum, ale bez skutku. Przegapiłem jakąś sztuczkę? Czy jest to w ogóle możliwe, skoro obracam kontekst, a nie Obraz?
Desperacko chce, żeby to zadziałało, więc każda pomoc będzie mile widziana.Dave
EDIT : kilka razy pytano mnie O mój kod wiążący, więc oto on:
- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation {
// Calculate the width and height of the bounding rectangle using basic trig
CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation));
CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation));
// Calculate the position of the origin
CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth) / 2);
CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight) / 2);
// Return the rectangle
return CGRectMake(newX, newY, newWidth, newHeight);
}
1 answers
OK-w końcu to zrobiłem. Wszelkie uwagi na temat poprawności byłyby przydatne... potrzebne tłumaczenie, obrót, podziałka i przesunięcie od pozycji rect rysunku, aby to działa. Kod jest tutaj:
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
// Draw the image into the context
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);
// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-24 14:24:40