Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIImageView:获取转换的UIImage_Ios_Uiimageview_Core Graphics_Transform - Fatal编程技术网

Ios UIImageView:获取转换的UIImage

Ios UIImageView:获取转换的UIImage,ios,uiimageview,core-graphics,transform,Ios,Uiimageview,Core Graphics,Transform,我正在使用以下命令旋转UIImageView: [UIView beginAnimations:nil context:nil]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; myImgView.transform = CGAffineTransformMakeRotation(angle*M_PI/180); [UIView com

我正在使用以下命令旋转UIImageView:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
myImgView.transform = CGAffineTransformMakeRotation(angle*M_PI/180);
[UIView commitAnimations];
现在我想得到旋转的图像。我能做这个吗?我试着用CoreGraphics,使用CGImageRotatedByAngle函数,从已接受的答案中获得它,但它将坐标转换得非常不正确(最后我们得到了第三个标记为错误的屏幕截图)。如您所见,图像将丢失其原始大小并被裁剪。我的目标是获得像原始大小的中央镜头一样的图像,并旋转指定的角度。我的意思是要求旋转形状的大小保持不变,但新图像当然会比初始图像大


好的,我再次回答我自己的问题,希望它对将来的人有用。。。 回到CGImageRotatedByAngle,正如我之前所说的,它提供了几乎所需的效果,但坐标系的转换顺序必须稍微改变。以下是我获得期望结果的方法:

- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle {

    CGFloat angleInRadians = angle * (M_PI / 180);
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                                   rotatedRect.size.width,
                                                   rotatedRect.size.height,
                                                   8,
                                                   0,
                                                   colorSpace,
                                                   kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, YES);
    CGContextSetShouldAntialias(bmContext, YES);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM( bmContext, 0.5f * rotatedRect.size.width, 0.5f * rotatedRect.size.height ) ;
    CGContextRotateCTM(bmContext, angleInRadians);
    CGContextDrawImage(bmContext, CGRectMake(-imgRect.size.width* 0.5f, -imgRect.size.height * 0.5f,
                                             imgRect.size.width,
                                             imgRect.size.height),
                       imgRef);
    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);
    [(id)rotatedImage autorelease];

    return rotatedImage;
}

这对我有用,还有另一种解决方法

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
    CGRect imgRect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);
    CGSize rotatedSize = rotatedRect.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedRect.size);
    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, DegreesToRadians(degrees));

    // Now, draw the rotated/scaled image into the context
    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 *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

以下是heximal的swift 4.1版本的答案

extension CGImage {

    func rotated(by angle: CGFloat) -> CGImage? {
        let angleInRadians = angle * .pi / 180

        let imgRect = CGRect(x: 0, y: 0, width: width, height: height)
        let transform = CGAffineTransform.identity.rotated(by: angleInRadians)
        let rotatedRect = imgRect.applying(transform)
        let colorSpace = CGColorSpaceCreateDeviceRGB()

        guard let bmContext = CGContext(
            data: nil,
            width: Int(rotatedRect.size.width),
            height: Int(rotatedRect.size.height),
            bitsPerComponent: 8,
            bytesPerRow: 0,
            space: colorSpace,
            bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue)
            else {
                return nil
        }

        bmContext.setAllowsAntialiasing(true)
        bmContext.setShouldAntialias(true)
        bmContext.interpolationQuality = .high
        bmContext.translateBy(x: rotatedRect.size.width * 0.5, y: rotatedRect.size.height * 0.5)
        bmContext.rotate(by: angleInRadians)
        let drawRect = CGRect(
            origin: CGPoint(x: -imgRect.size.width * 0.5, y: -imgRect.size.height * 0.5),
            size: imgRect.size)
        bmContext.draw(self, in: drawRect)

        guard let rotatedImage = bmContext.makeImage() else { return nil }

        return rotatedImage
    }

}

回答你自己的问题不要感到难过。如果答案提供了一个准确、相关的问题答案,请随意接受。看见