Wie drehe ich ein UIImage um 90 Grad?
Ich habe einUIImage
das istUIImageOrientationUp
(Hochformat), das ich gegen den Uhrzeigersinn um 90 Grad drehen möchte (ins Querformat). Ich möchte kein verwendenCGAffineTransform
. Ich möchte die Pixel derUIImage
Position tatsächlich verschieben. Ich verwende einen Codeblock (siehe unten), der ursprünglich zur Größenänderung von a bestimmt warUIImage
um dies zu tun. Ich habe eine Zielgröße als die aktuelle Größe desUIImage
aber ich bekomme einen fehler:
(Fehler): CGBitmapContextCreate: ungültige Datenbytes / Zeile: sollte mindestens 1708 für 8 ganzzahlige Bits / Komponente, 3 Komponenten, kCGImageAlphaPremultipliedLast sein.
(Ich erhalte keine Fehlermeldung, wenn ich eine KLEINERE Größe als Zielgröße angeben möchte.) Wie kann ich meineUIImage
90 Grad im Uhrzeigersinn mit nur Kerngrafikfunktionen unter Beibehaltung der aktuellen Größe?
-(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, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}