调整大小后旋转45度的图像数据

问题描述:

我有一段代码利用ImageIO库来调整图像的大小,使其在上传到Parse时变得更小。上传时,图片逆时针旋转45度。调整大小后旋转45度的图像数据

let imageBytes = UIImageJPEGRepresentation(imageView.image, 1.0) 
let size = CGSizeMake(1024, 1024) 
if let imageSource = CGImageSourceCreateWithData(imageBytes, nil) { 

    let options: [NSString: AnyObject] = [ 
    kCGImageSourceThumbnailMaxPixelSize: NSNumber(double: Double(max(size.width, size.height))/2.0), 
    kCGImageSourceCreateThumbnailFromImageIfAbsent: true, 
    kCGImageSourceCreateThumbnailWithTransform: false 
    ] 

    //2. Recreate the Image that has been scaled 
    let scaledImage = UIImage(CGImage: CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)) 
    let scaledImageBytes = UIImageJPEGRepresentation(scaledImage, 1.0) 

    let metaData = [ 
    "imageBytes": scaledImageBytes, //for some reason, scaledImageBytes causes a 90 degree rotation on the picture. Using unscaled image for now. 
    ] 
    //3. upload the image to Parse 
    PFCloud.callFunctionInBackground("uploadImage", withParameters: metaData) { message, error in 
    if let error = error { 
     println("error uploading: \(error)") 
    } else if let message = message as? String { 
     println("success: \(message)") 
    } 
    } 
} 

我该如何解决这个问题?

检查此功能正常化图像

func normalizeImage(raw: UIImage) -> UIImage { 
    if raw.imageOrientation == UIImageOrientation.Up { 
     return raw 
    } 
    UIGraphicsBeginImageContextWithOptions(raw.size, false, raw.scale) 
    raw.drawInRect(CGRect(origin: CGPointZero, size: raw.size)) 
    var normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return normalizedImage 
}