在IOS中将旋转的图像存储到本地

时间:2017-10-17 07:37:16

标签: ios image rotation

我有一个集合视图,其中我们从图库中放置图像的单元格。在单元格上有一个按钮,当我们单击它时旋转图像。现在,当我单击图像时,它会旋转图像,但是当我将它发送到服务器时,它不会像我选择的那样进入旋转角度,而是来自图库或相机。当我从图库中选择一个图像时,它会在图像数组中给出它,

 III (
   "<UIImage: 0x608000299b90>, {1000, 624}"
 )

在图像数组的路径中,它来了,

FFF is(
  "/Users/Apple/Library/Developer/CoreSimulator/Devices/23D54E17-36FB-4397-BA5E-09A05EB0EBF3/data/Containers/Data/Application/C1D07D05-11B3-4D49-8D91-884FC3DA1A06/Documents/E7IME.png"
)

现在我想存储图像的旋转条件并将其发送到具有相同旋转条件的服务器。但当我点击按钮时,控制台中没有任何内容,只是图像旋转。

enter image description here

2 个答案:

答案 0 :(得分:0)

您必须旋转图像并保存该状态,请尝试以下操作:

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)

-(UIImage *)rotateImage:(UIImage*)src byRadian:(CGFloat)radian
{
    CGRect rect = CGRectApplyAffineTransform(CGRectMake(0,0, src.size.width, src.size.height), CGAffineTransformMakeRotation(radian));
    CGSize rotatedSize = rect.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, radian);

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

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

用法:

UIImage *rotatedImage = [self rotateImage:[UIImage imageNamed:@"urImageName"] byRadian:DEGREES_TO_RADIANS(90)];
[self.image setImage:rotatedImage];

注意:您必须发送rotateImage。

答案的灵感来自:https://stackoverflow.com/a/20860719/4004429

答案 1 :(得分:0)

您可以使用

将修改后的图像(imageView)保存在图库中

UIImageWriteToSavedPhotosAlbum(imageView, nil, nil, nil);

nil序列是完成块,您可以阅读用法here