Swift如何防止图像旋转和淀粉

时间:2018-08-14 08:08:46

标签: 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

}

2 个答案:

答案 0 :(得分:2)

我使用此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
        }
    }
}

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

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)
}

Ps: 您可以根据需要更改方向类型。只需更改:

 if self.imageOrientation == UIImageOrientation.up {//change orientation
        return self
    }

这些都是用度数说明的方向类型,

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
    };

您可以参考UIImage.Orientation Apple Documentation以获得更多信息。

答案 1 :(得分:0)

在某些UIImage中,其位图表示形式(CGImage)会旋转,UIImage会根据imageOrientation属性(未将其忽略)绘制为未旋转。

您可以这样写:

func pixelCalculator() {
    let image = originalImage

    //### Keep scale and orientation.
    let scale = originalImage.scale
    let orientation = originalImage.imageOrientation

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

    let bitsPerComponent = 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 rect = CGRect(origin: .zero, 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)

    //### Create a UIImage with the same scale and orientation as the originalImage.
    let outImage = UIImage(cgImage: outContext!.makeImage()!, scale: scale, orientation: orientation)
    imageView.image = outImage
}

但是,如果您想修改pixels并且算法取决于方向,则最好使用Enea Dume的答案。