快速如何防止图像旋转和淀粉

KUR*_*UKI 1 swift

该函数应该获取图像并将其转换为数据,然后将数据转换回图像。当我使用图像库中的图像或以横向模式拍摄照片时,效果很好。但是,当我以肖像模式拍摄照片时,图像在转换回图像时将会旋转和拉伸。有什么办法可以防止这种情况发生吗?

func pixelCalculator (){
    let image = originalImage

    let height = Int((image.size.height))
    print(height)
    let width = Int((image.size.width))

    let bitsPerComponent = Int(8)
    let bytesPerRow = 4 * width
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let rawData = UnsafeMutablePointer<RGBAPixel>.allocate(capacity: (width * height))
    let bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue
    let CGPointZero = CGPoint(x: 0, y: 0)
    let rect = CGRect(origin: CGPointZero, size: (image.size))



    let imageContext = CGContext(data: rawData, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)

    imageContext?.draw(image.cgImage!, in: rect)

    let pixels = UnsafeMutableBufferPointer<RGBAPixel>(start: rawData, count: width * height)

    let outContext = CGContext(data: pixels.baseAddress, width: width, height: height, bitsPerComponent: bitsPerComponent,bytesPerRow: bytesPerRow,space: colorSpace,bitmapInfo: bitmapInfo,releaseCallback: nil,releaseInfo: nil)

    let outImage = UIImage(cgImage: outContext!.makeImage()!)
    imageView.image = outImage

}
Run Code Online (Sandbox Code Playgroud)

Ene*_*ume 6

我用它extension来修复图像的方向:

extension UIImage {
    func fixOrientation() -> UIImage {
        if self.imageOrientation == UIImageOrientation.up {
            return self
        }
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
        if let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() {
            UIGraphicsEndImageContext()
            return normalizedImage
        } else {
            return self
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我使用这个扩展的方式:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
        let fixedPickedImage = pickedImage.fixOrientation()//this is the new fixed orientation image.
    }
    parent?.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

Ps:您可以根据您的要求更改方向类型。只是改变:

 if self.imageOrientation == UIImageOrientation.up {//change orientation
        return self
    }
Run Code Online (Sandbox Code Playgroud)

这些都是以度数解释的方向类型,

typedef NS_ENUM(NSInteger, UIImageOrientation) {
        UIImageOrientationUp,            // default orientation
        UIImageOrientationDown,          // 180 deg rotation
        UIImageOrientationLeft,          // 90 deg CCW
        UIImageOrientationRight,         // 90 deg CW
        UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
        UIImageOrientationDownMirrored,  // horizontal flip
        UIImageOrientationLeftMirrored,  // vertical flip
        UIImageOrientationRightMirrored, // vertical flip
    };
Run Code Online (Sandbox Code Playgroud)

您可以参考UIImage.Orientation Apple 文档以获取更多信息。