У меня есть UIImage, который я получаю от UIImagePickerController. Когда я получаю изображение из метода - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
, я помещаю его прямо в UIImageView для предварительного просмотра и предоставляю пользователю возможность сохранять или отбрасывать изображение.
Когда пользователь выбирает параметр сохранения, приложение получает данные png для изображения и сохраняет его в каталоге документов. Но то, что происходит при одной из двух возможных ориентаций устройства (только для ландшафта), заключается в том, что изображение сохраняется с ног на голову (точнее, повернуто на 180 градусов от того, что должно быть). Поэтому, когда я загружаю изображение в галерею, оно отображается вверх ногами.
(см. нижнее левое изображение в галерее)
Я работал так, что необработанные данные изображения в UIImage из UIImagePickerController не вращаются, скорее, ориентация сохраняется как свойство объекта и применяется только при отображении. Поэтому я пытаюсь повернуть CGImage, связанный с UIImage, используя код, который я нашел в Интернете. Но, похоже, это абсолютно не влияет на изображение. Код, который я использую, следующий:
- (void)rotateImage:(UIImage*)image byRadians:(CGFloat)rads
{
// calculate the size of the rotated view containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(rads);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image you want to rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// Rotate the image context
CGContextRotateCTM(bitmap, rads);
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(image.size.width / 2, image.size.height / 2, image.size.width, image.size.height), [image CGImage]);
image = UIGraphicsGetImageFromCurrentImageContext();
image = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDown];
UIGraphicsEndImageContext();
}
Я хотел бы знать, почему этот код не работает, поскольку каждый фрагмент кода, который я нахожу в Интернете, чтобы сделать это вращение изображения, кажется, не работает.