UIImage iPhone Повернуть на 37,8 градусов - PullRequest
2 голосов
/ 11 января 2010

После того, как я загрузил изображение с устройства, мне нужно повернуть его на 37,8 градуса, а затем отобразить в виде.

Есть ли в Objective-C функция, которая может вращать изображение?

Ian

Ответы [ 3 ]

6 голосов
/ 11 января 2010

Для поворота вида:

imageView.transform = CGAffineTransformMakeRotation(37.8°);

Чтобы повернуть изображение,

  1. Рассчитайте ширину и высоту, которые будут занимать изображения после поворота.
  2. Создать CGContext с помощью UIGraphicsBeginImageContext.
  3. CGContextRotateCTM(UIGraphicsGetCurrentContext(), 37.8°);
  4. [yourImage drawAtPoint:...];
  5. UIGraphicsGetImageFromCurrentImageContext(); и используйте вместо этого изображение.
  6. Освободить контекст.
1 голос
/ 02 марта 2015

Чтобы повернуть изображение, попробуйте это:

-(IBAction)rotateImageClick:(id)sender{

    UIImage *image2=[[UIImage alloc]init];
    image2 = [self imageRotatedByDegrees:self.roateImageView.image deg:(90)]; //Angle by 90 degree
    self.roateImageView.image = image2;
    imgData= UIImageJPEGRepresentation(image2,0.9f);

}

Этот метод позволяет повернуть изображение на произвольную величину:

- (UIImage *)imageRotatedByDegrees:(UIImage*)oldImage deg:(CGFloat)degrees{

    // calculate the size of the rotated view's containing box for our drawing space
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,oldImage.size.width, oldImage.size.height)];

    CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);
    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 so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, (degrees * M_PI / 180));

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-oldImage.size.width / 2, -oldImage.size.height / 2, oldImage.size.width, oldImage.size.height), [oldImage CGImage]);

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

}

См. эту ссылку для получения дополнительной информации.

1 голос
/ 11 января 2010

Да, смотрите мой ответ на этот вопрос: Вопрос о вращении слайдера

Чтобы преобразовать градусы в радианы (для аргумента positionInRadians), используйте эту функцию:

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
...