İçinde bir görüntü bulunan bir UIImageView var. UIImageView ile CGAffineTransformMakeRotation (açı) 'nın dönüş açısını radyan cinsinden açı olarak ayarlayarak görüntülemeden önce görüntüyü döndürdüm.Döndürülmüş bir UIImageView'dan UIImage oluşturma
Görünümümde görebildiğim döndürülen sürüme karşılık gelen başka bir UIImage oluşturmak istiyorum. Görüntü merkezi etrafında
- (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;
}
döndürülmüş değildir Ancak: Resim bağlamı çevirerek, neredeyse oradayım
Ben döndürülen resmi olsun. Merkezim etrafında dönmesini sağlamak için döndürme ile birleştirilmiş her türlü dönüşümü denedim ama boşuna. Bir numara eksik mi? Bu, görüntüyü değil bağlamı döndürdüğümden beri mümkün mü?
Şimdi bu işi yapmak için çaresiz olmak, böylece herhangi bir yardım takdir edilecektir.
Dave
DÜZENLEME: Öyle işte burada, benim boundingRect kodu için birkaç kez istendi:
- (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);
}
bu 'getBoundingRectAfterRotation' işlevi nedir? – bmdhacks
Merhaba bmdhacks getBoundingRectAfterRotation oluşturduğum bir yöntemdir, bu yüzden herhangi bir Cocoa-Touch sınıfında bulamıyorsunuz. Argümanlar olarak bir doğrultu ve açıyı alır ve verilen açı tarafından döndürüldüğünde orijinal rect'i kapsayacak yeni bir CGRect döndürür. –
Merhaba popüler talepten dolayı bmdhacks, orijinal sorgunun bir düzenlemesinde yöntemi ekledi. –