15

UIImagePickerControllerから取得しているUIImageがあります。メソッドから画像を受け取ったら- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info、プレビュー用にUIImageViewに直接配置し、ユーザーに画像を保存するか破棄するかを選択できるようにします。

プレビュー画面

ユーザーが保存オプションを選択すると、アプリケーションは画像のpngデータを取得し、そのドキュメントディレクトリに保存します。ただし、2つの可能なデバイスの向き(横向きのみ)のいずれかで発生するのは、画像が上下逆に保存されることです(より具体的には、本来あるべき位置から180度回転します)。そのため、ギャラリーに画像を読み込むと、画像が上下逆に表示されます。

(ギャラリーの左下の画像を参照してください)

左下の画像を参照してください

UIImagePickerControllerからのUIImageの生の画像データは回転せず、方向はオブジェクトのプロパティとして保存され、表示時にのみ適用されるようになりました。そのため、オンラインで見つけたコードを使用して、UIImageに関連付けられたCGImageを回転させようとしています。しかし、それは画像にまったく影響を与えていないようです。私が使用しているコードは次のとおりです。

- (void)rotateImage:(UIImage*)image byRadians:(CGFloat)rads
{   
    // calculate the size of the rotated view's containing box for our drawing space 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(rads);
    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 you want to rotate and scale around the center. 
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

    // Rotate the image context
    CGContextRotateCTM(bitmap, rads);

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

    image = UIGraphicsGetImageFromCurrentImageContext();
    image = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDown];
    UIGraphicsEndImageContext();
}

この画像の回転を行うためにオンラインで見つけたすべてのコードが機能していないように見えるため、このコードが機能しない理由を知りたいと思います。

4

6 に答える 6

26
-(UIImage*) rotate:(UIImage*) src andOrientation:(UIImageOrientation)orientation
{
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context=(UIGraphicsGetCurrentContext());

    if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, 90/180*M_PI) ;
    } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, -90/180*M_PI);
    } else if (orientation == UIImageOrientationDown) {
        // NOTHING
    } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, 90/180*M_PI);
    }

    [src drawAtPoint:CGPointMake(0, 0)];
    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}

これを使用してください..それは完璧に動作します!UIImagePickerから画像を選択するときは、必ずこの関数を使用してください。

例:

UIImage *image;
image = [self rotate:image andOrientation:image.imageOrientation];

ドキュメントディレクトリから写真を選択する場所でこの機能を使用してください。

于 2012-05-11T08:58:22.937 に答える
10

UIImageCGImageバッキングデータの回転とミラーリング-Swift

私は最近、UIImageOrientationパラメーターを尊重するためにAPIに依存するのではなく、目的の方向に一致するようにUIImage裏付けを修正したいと考えました。CGImage

func createMatchingBackingDataWithImage(imageRef: CGImage?, orienation: UIImageOrientation) -> CGImage?
{
    var orientedImage: CGImage?

    if let imageRef = imageRef {
        let originalWidth = imageRef.width
        let originalHeight = imageRef.height
        let bitsPerComponent = imageRef.bitsPerComponent
        let bytesPerRow = imageRef.bytesPerRow

        let bitmapInfo = imageRef.bitmapInfo

        guard let colorSpace = imageRef.colorSpace else {
            return nil
        }

        var degreesToRotate: Double
        var swapWidthHeight: Bool
        var mirrored: Bool
        switch orienation {
        case .up:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = false
            break
        case .upMirrored:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = true
            break
        case .right:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .rightMirrored:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = true
            break
        case .down:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = false
            break
        case .downMirrored:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = true
            break
        case .left:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .leftMirrored:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = true
            break
        }
        let radians = degreesToRotate * Double.pi / 180.0

        var width: Int
        var height: Int
        if swapWidthHeight {
            width = originalHeight
            height = originalWidth
        } else {
            width = originalWidth
            height = originalHeight
        }

        let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
        contextRef?.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)
        if mirrored {
            contextRef?.scaleBy(x: -1.0, y: 1.0)
        }
        contextRef?.rotate(by: CGFloat(radians))
        if swapWidthHeight {
            contextRef?.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
        } else {
            contextRef?.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
        }
        contextRef?.draw(imageRef, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(originalWidth), height: CGFloat(originalHeight)))
        orientedImage = contextRef?.makeImage()
    }

    return orientedImage
}
于 2015-01-01T12:41:26.377 に答える
3

CGContextTranslateCTM(context, -rotatedSize.width/2, -rotatedSize.height/2);問題が発生しました。描画のrectの原点をrotatedViewBox.frame.origin.x、rotatedViewBox.frame.origin.yに設定するだけでなく、を使用してコンテキストを変換し直す必要があります 。このコードを使用してください。

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(0.3);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;



UIGraphicsBeginImageContext(rotatedSize);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM (context, 0.3);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, -rotatedSize.width/2, -rotatedSize.height/2);




CGContextDrawImage(context, CGRectMake(-rotatedViewBox.frame.origin.x, -rotatedViewBox.frame.origin.y, image.size.width, image.size.height), [image CGImage]);


UIImage *nn = UIGraphicsGetImageFromCurrentImageContext();

NSLog(@"size is %f, %f",nn.size.width,nn.size.height);
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(nn, nil,nil,nil);
于 2012-09-11T10:38:49.543 に答える
3

キャメロンローウェルパーマーSwift3バージョンの回答:

func createMatchingBackingDataWithImage(imageRef: CGImage?, orienation: UIImageOrientation) -> CGImage? {
    var orientedImage: CGImage?

    if let imageRef = imageRef {
        let originalWidth = imageRef.width
        let originalHeight = imageRef.height
        let bitsPerComponent = imageRef.bitsPerComponent
        let bytesPerRow = imageRef.bytesPerRow

        let colorSpace = imageRef.colorSpace
        let bitmapInfo = imageRef.bitmapInfo

        var degreesToRotate: Double
        var swapWidthHeight: Bool
        var mirrored: Bool
        switch orienation {
        case .up:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = false
            break
        case .upMirrored:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = true
            break
        case .right:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .rightMirrored:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = true
            break
        case .down:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = false
            break
        case .downMirrored:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = true
            break
        case .left:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .leftMirrored:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = true
            break
        }
        let radians = degreesToRotate * Double.pi / 180

        var width: Int
        var height: Int
        if swapWidthHeight {
            width = originalHeight
            height = originalWidth
        } else {
            width = originalWidth
            height = originalHeight
        }

        if let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace!, bitmapInfo: bitmapInfo.rawValue) {

            contextRef.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)
            if mirrored {
                contextRef.scaleBy(x: -1.0, y: 1.0)
            }
            contextRef.rotate(by: CGFloat(radians))
            if swapWidthHeight {
                contextRef.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
            } else {
                contextRef.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
            }
            contextRef.draw(imageRef, in: CGRect(x: 0, y: 0, width: originalWidth, height: originalHeight))

            orientedImage = contextRef.makeImage()
        }
    }

    return orientedImage
}

使用法:

let newCgImage = createMatchingBackingDataWithImage(imageRef: cgImageRef, orienation: .left)
于 2017-04-20T23:24:37.287 に答える
1

これは、以前の回答のリファクタリングにすぎません。

func correctImageOrientation(cgImage: CGImage?, orienation: UIImage.Orientation) -> CGImage? {
    guard let cgImage = cgImage else { return nil }
    var orientedImage: CGImage?

    let originalWidth = cgImage.width
    let originalHeight = cgImage.height
    let bitsPerComponent = cgImage.bitsPerComponent
    let bytesPerRow = cgImage.bytesPerRow
    let bitmapInfo = cgImage.bitmapInfo

    guard let colorSpace = cgImage.colorSpace else { return nil }

    let degreesToRotate = orienation.getDegree()
    let mirrored = orienation.isMirror()

    var width = originalWidth
    var height = originalHeight

    let radians = degreesToRotate * Double.pi / 180.0
    let swapWidthHeight = Int(degreesToRotate / 90) % 2 != 0

    if swapWidthHeight {
        swap(&width, &height)
    }

    let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

    context?.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)
    if mirrored {
        context?.scaleBy(x: -1.0, y: 1.0)
    }
    context?.rotate(by: CGFloat(radians))
    if swapWidthHeight {
        swap(&width, &height)
    }
    context?.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)

    context?.draw(cgImage, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(originalWidth), height: CGFloat(originalHeight)))
    orientedImage = context?.makeImage()

    return orientedImage
}

extension UIImage.Orientation {
    func getDegree() -> Double {
        switch self {
        case .up, .upMirrored:
            return 0.0
        case .right, .rightMirrored:
            return 90.0
        case .down, .downMirrored:
            return 180.0
        case .left, .leftMirrored:
            return -90.0
        default:
            return 0
        }
    }

    func isMirror() -> Bool {
        switch self {
        case .up, .right, .down, .left:
            return false
        case .leftMirrored, .upMirrored, .downMirrored, .rightMirrored:
            return true
        default:
            return false
        }
    }
}
于 2020-05-14T08:04:03.677 に答える
1

これらすべての回答の問題は、UIKitに依存していることです。iOSとmacOSの両方を対象としたSwiftUIユニバーサルアプリに取り組んでいます。キャメロンローウェルパーマーの答えは、いくつかの変更を加えて機能します:

  1. bytesPerRowの計算は、高さと幅を入れ替えた後に実行する必要があります。そうしないと、CGImageContextが不正なbytesPerRowについてうなり声を上げます。

  2. UIImageOrientationをCGImagePropertyOrientationに変更し、左右の回転の符号を入れ替えます

これが私のバージョンで、iOSとmacOSの両方に適しています。

extension CGImage {
func rotating(to orientation: CGImagePropertyOrientation) -> CGImage? {
    var orientedImage: CGImage?

    let originalWidth = self.width
    let originalHeight = self.height
    let bitsPerComponent = self.bitsPerComponent
    let bitmapInfo = self.bitmapInfo

    guard let colorSpace = self.colorSpace else {
        return nil
    }

    var degreesToRotate: Double
    var swapWidthHeight: Bool
    var mirrored: Bool

    switch orientation {
    case .up:
        degreesToRotate = 0.0
        swapWidthHeight = false
        mirrored = false
        break
    case .upMirrored:
        degreesToRotate = 0.0
        swapWidthHeight = false
        mirrored = true
        break
    case .right:
        degreesToRotate = -90.0
        swapWidthHeight = true
        mirrored = false
        break
    case .rightMirrored:
        degreesToRotate = -90.0
        swapWidthHeight = true
        mirrored = true
        break
    case .down:
        degreesToRotate = 180.0
        swapWidthHeight = false
        mirrored = false
        break
    case .downMirrored:
        degreesToRotate = 180.0
        swapWidthHeight = false
        mirrored = true
        break
    case .left:
        degreesToRotate = 90.0
        swapWidthHeight = true
        mirrored = false
        break
    case .leftMirrored:
        degreesToRotate = 90.0
        swapWidthHeight = true
        mirrored = true
        break
    }

    let radians = degreesToRotate * Double.pi / 180.0

    var width: Int
    var height: Int

    if swapWidthHeight {
        width = originalHeight
        height = originalWidth
    } else {
        width = originalWidth
        height = originalHeight
    }

    let bytesPerRow = (width * bitsPerPixel) / 8

    let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

    contextRef?.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)

    if mirrored {
        contextRef?.scaleBy(x: -1.0, y: 1.0)
    }

    contextRef?.rotate(by: CGFloat(radians))

    if swapWidthHeight {
        contextRef?.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
    } else {
        contextRef?.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
    }

    contextRef?.draw(self, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(originalWidth), height: CGFloat(originalHeight)))

    orientedImage = contextRef?.makeImage()

    return orientedImage
}

}

于 2021-06-17T22:58:45.603 に答える