我有一个UIImage是UIImageOrientationUp(纵向),我想通过90度逆时针旋转(景观).我不想用CGAffineTransform.我想要UIImage实际转移位置的像素.我正在使用一段代码(如下所示),最初是为了调整大小UIImage来执行此操作.我将目标大小设置为当前大小UIImage但是我得到一个错误:
(错误):CGBitmapContextCreate:无效数据字节/行:对于8个整数位/组件,3个组件,kCGImageAlphaPremultipliedLast,应该至少为1708.
(每当我提供SMALLER尺寸作为目标尺寸BTW时,我都不会收到错误).如何UIImage在保留当前尺寸的同时仅使用核心图形功能旋转我的90度CCW?
-(UIImage*)reverseImageByScalingToSize:(CGSize)targetSize:(UIImage*)anImage
{
UIImage* sourceImage = anImage;
CGFloat targetWidth = targetSize.height;
CGFloat targetHeight = targetSize.width;
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaNoneSkipLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, targetWidth, …Run Code Online (Sandbox Code Playgroud) 该函数应该获取图像并将其转换为数据,然后将数据转换回图像。当我使用图像库中的图像或以横向模式拍摄照片时,效果很好。但是,当我以肖像模式拍摄照片时,图像在转换回图像时将会旋转和拉伸。有什么办法可以防止这种情况发生吗?
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 …Run Code Online (Sandbox Code Playgroud)