【问题标题】:Rotate Image in ImageView and save to server在 ImageView 中旋转图像并保存到服务器
【发布时间】:2015-09-30 06:54:02
【问题描述】:

我们如何在图像视图中旋转图像并在图像上应用手势并将该图像以与图像相同的状态保存到服务器

- (IBAction)imageMove:(id)sender {
    static int numRot = 0;

    myimage.transform = CGAffineTransformMakeRotation(M_PI_2 * numRot);
    ++numRot;
}

通过这段代码,我可以将图像视图旋转 90 度

【问题讨论】:

  • 不在这里,您正在旋转 imagview 而不是图像视图中的图像,并且在该图像上应用手势,您必须将该图像以相同状态保存到服务器
  • 好的,我得到了答案,我正在编辑,请检查。
  • 请检查我的答案,我正在使用它及其工作。

标签: ios imageview gesture


【解决方案1】:

使用这个:-

@interface UIImage (RotationMethods)
- (UIImage *)rotateImageByDegree:(CGFloat)degrees;
@end

@implementation UIImage (RotationMethods)

static CGFloat getRadianFromDegree(CGFloat degrees) 

{返回度数 * M_PI / 180;};

- (UIImage *) rotateImageByDegree:(CGFloat)degrees 
{   
    UIView *rotatedImageView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(getRadianFromDegree(degrees));
    rotatedImageView.transform = t;
    CGSize rotatedSize = rotatedImageView.frame.size;
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
    CGContextRotateCTM(bitmap, getRadianFromDegree(degrees));
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
    UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return rotatedImage;

}

@end

【讨论】:

    【解决方案2】:

    试试这个,这是最好的解决方案,我也用过。

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

    你可以这样称呼,

    [self imageRotatedByDegrees:<your Image> deg:90];
    

    【讨论】:

    • 感谢接受,以便其他人可以使用它,它也会帮助他们。
    【解决方案3】:

    一旦你旋转了 UIImageView,你可以使用类似的东西获得UIImage

    CGRect imageViewRect = [imageView bounds];
    UIGraphicsBeginImageContext(imageViewRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [imageView.layer renderInContext:context];   
    UIImage *imageObject = UIGraphicsGetImageFromCurrentImageContext();
    

    现在您可以将imageObject 保存到您的服务器

    【讨论】:

      【解决方案4】:

      我相信最简单的方法(也是线程安全的)是:

       UIImage * LandscapeImage = [UIImage imageNamed: imgname];
          UIImage * PortraitImage = [[UIImage alloc] initWithCGImage: LandscapeImage.CGImage
                                                               scale: 1.0
                                                         orientation: UIImageOrientationRight];
      

      【讨论】:

        【解决方案5】:

        试试这个代码

        Image.transform=CGAffineTransformMakeRotation(M_PI / 2);
        
        self.imageview.image=Image;
        

        【讨论】:

          猜你喜欢
          • 2023-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多