旋转 UIImage 的问题

发布于 2024-09-26 17:55:39 字数 1114 浏览 3 评论 0原文

我的代码如下。有谁知道发生了什么事?

抱歉信息不完整。图像就这样消失了!

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *imCaptured = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    CGImageRef cgirCropped = CGImageCreateWithImageInRect(imCaptured.CGImage, CGRectMake(700.0f, 430.0f, 650.0f, 650.0f));
    UIImage *imCropped = [UIImage imageWithCGImage:cgirCropped];

    UIGraphicsBeginImageContext(imCropped.size);
    CGContextRef cgcrRotation = UIGraphicsGetCurrentContext();

    switch (imCaptured.imageOrientation) {
        case UIImageOrientationUp:
            CGContextRotateCTM(cgcrRotation, (180 * M_PI / 180));
            break;
        case UIImageOrientationDown:

            break;
        case UIImageOrientationLeft:

            break;
        case UIImageOrientationRight:
            CGContextRotateCTM(cgcrRotation, (90 * M_PI / 180));
            break;
    }
    [imCropped drawAtPoint:CGPointMake(0.0f, 0.0f)];
    [imvPreview setImage:UIGraphicsGetImageFromCurrentImageContext()];
}

My code is below. Does anyone know what is happening?

Sorry about the incomplete information. The image just disappears!

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *imCaptured = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    CGImageRef cgirCropped = CGImageCreateWithImageInRect(imCaptured.CGImage, CGRectMake(700.0f, 430.0f, 650.0f, 650.0f));
    UIImage *imCropped = [UIImage imageWithCGImage:cgirCropped];

    UIGraphicsBeginImageContext(imCropped.size);
    CGContextRef cgcrRotation = UIGraphicsGetCurrentContext();

    switch (imCaptured.imageOrientation) {
        case UIImageOrientationUp:
            CGContextRotateCTM(cgcrRotation, (180 * M_PI / 180));
            break;
        case UIImageOrientationDown:

            break;
        case UIImageOrientationLeft:

            break;
        case UIImageOrientationRight:
            CGContextRotateCTM(cgcrRotation, (90 * M_PI / 180));
            break;
    }
    [imCropped drawAtPoint:CGPointMake(0.0f, 0.0f)];
    [imvPreview setImage:UIGraphicsGetImageFromCurrentImageContext()];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2024-10-03 17:55:39

我还没有测试过代码,但我认为它绕着原点旋转,所以你也需要翻译,比如:

...
case UIImageOrientationUp:
        CGContextRotateCTM(cgcrRotation, (180 * M_PI / 180));
        CGContextTranslateCTM(ctxt, -imCropped.size.width, -imCropped.size.height);
        break;
...

I haven't tested the code, but I think it rotates around the origin, so you need to translate as well, something like:

...
case UIImageOrientationUp:
        CGContextRotateCTM(cgcrRotation, (180 * M_PI / 180));
        CGContextTranslateCTM(ctxt, -imCropped.size.width, -imCropped.size.height);
        break;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文