ฉันหมุนภาพกลับหัว แต่จะพลิกภาพในแนวนอนได้อย่างไร

ฉันโหลดภาพจากม้วนฟิล์ม แต่ภาพนี้กลับหัว ฉันก็เลยเขียนวิธีหมุนมัน

CGImageRef imageRef = [image CGImage];

float width = CGImageGetWidth(imageRef);
float height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Byte *rawData = malloc(height * width * 4);
Byte bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
Byte bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
int byteIndex = 0;
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

Byte *rawData2 = malloc(height * width * 4);

for (int i = 0 ; i < width * height ; i++) {
    int index = (width * height) * 4;
    rawData2[byteIndex + 0] = rawData[index - byteIndex + 0];
    rawData2[byteIndex + 1] = rawData[index - byteIndex + 1];
    rawData2[byteIndex + 2] = rawData[index - byteIndex + 2];
    rawData2[byteIndex + 3] = rawData[index - byteIndex + 3];

    byteIndex += 4;
}

CGContextRef ctx = CGBitmapContextCreate(rawData2, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), 8, CGImageGetBytesPerRow( imageRef ), CGImageGetColorSpace( imageRef ),kCGImageAlphaPremultipliedLast );

imageRef = CGBitmapContextCreateImage (ctx);
image = [UIImage imageWithCGImage:imageRef];

CGContextRelease(context);

return image;

ไม่เป็นไร แต่ตอนนี้ฉันต้องพลิกมันในแนวนอน และฉันไม่รู้ว่าจะทำยังไง ฉันพยายามทำวันที่สองนี้

ขอบคุณสำหรับความช่วยเหลือ


person Tomasz Szulc    schedule 29.07.2012    source แหล่งที่มา


คำตอบ (1)


คุณได้ลองสิ่งนี้แล้ว:

imageView.transform = CGAffineTransformMakeScale(-1, 1);

?

คุณยังสามารถหมุนได้โดยใช้การแปลง:

imageView.transform = CGAffineTransformMakeRotation(M_PI);

คุณสามารถเชื่อมการเปลี่ยนแปลงทั้งสองเข้าด้วยกันในลักษณะนี้:

imageView.transform = CGAffineTransformRotation(CGAffineTransformMakeScale(-1, 1), M_PI);

หากคุณต้องการสร้างออบเจ็กต์ UIImage ของคุณเอง แทนที่จะจัดการมุมมองและการแปลง ฉันยังคงแนะนำให้คุณใช้วิธีการที่อธิบายไว้ข้างต้นเพื่อทำให้มุมมองวาดภาพตามที่คุณต้องการ จากนั้นแปลงเนื้อหา UIView ของคุณให้เป็นออบเจ็กต์ UIImage:

UIGraphicsBeginImageContext(rect.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
person sergio    schedule 29.07.2012
comment
ฉันต้องแปลง UIImage และวาดบน CGContextRef ดังนั้น .transform จึงไม่พร้อมใช้งาน ฉันพยายามเพิ่ม UIImage ให้กับ UIImageView หมุนและสร้าง image = imageView.image แต่มันไม่ทำงาน - person Tomasz Szulc; 29.07.2012
comment
โปรดดูการแก้ไขของฉันเกี่ยวกับวิธีการแปลงเนื้อหามุมมองเป็นรูปภาพ - person sergio; 29.07.2012
comment
มันใช้งานได้ แต่ฉันตั้งค่า imageview.frame = CGRectMake(111,122,768,1024) และแสดงบริบทที่ 0,0,768,1024 ข้อเสนอแนะใด ๆ ? - person Tomasz Szulc; 29.07.2012
comment
โอเค ขอบคุณสำหรับความช่วยเหลือ ฉันเพิ่ม imageView ลงใน subView และเรนเดอร์ - person Tomasz Szulc; 29.07.2012