2010-02-10 29 views
6

Tôi đang tạo ảnh ghép kết hợp với các yếu tố từ các hình ảnh khác. Dưới đây là một số nghệ thuật ASCII để giải thích những gì tôi đang làm:Nhiều đường cắt bớt để tạo ảnh ghép trong đồ họa lõi

Given images A, B and C, 
AAA, BBB, CCC 
AAA, BBB, CCC 
AAA, BBB, CCC 

I take part of A, part of B and part of C as columns: 

Axx, xBx, xxC 
Axx, xBx, xxC 
Axx, xBx, xxC 

...and combine them in one image like this: 

ABC 
ABC 
ABC 

where the first 1/3rd of the image is a colum of A's pic, the middle is a column of B's pic and the last is a column of C's pic. 

Tôi có một số mã được viết nhưng nó chỉ hiển thị cột đầu tiên và không phải là phần còn lại ... Tôi nghĩ rằng tôi phải xóa clipping bằng cách nào đó, bt Tôi không chắc chắn làm thế nào để làm điều đó hoặc cho dù điều này thậm chí là cách tiếp cận tốt nhất.

+ (UIImage *)collageWithSize:(NSInteger)size fromImages:(NSArray *)images { 
    NSMutableArray *selectedImages = [NSMutableArray array]; 
    [selectedImages addObjectsFromArray:images]; 

    // use the selectedImages for generating the thumbnail 
    float columnWidth = (float)size/(float)[selectedImages count]; 

    //create a context to do our clipping in 
    UIGraphicsBeginImageContext(CGSizeMake(size, size)); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    for (int i = 0; i < [selectedImages count]; i++) { 
     // get the current image 
     UIImage *image = [selectedImages objectAtIndex:i]; 

     //create a rect with the size we want to crop the image to 
     CGRect clippedRect = CGRectMake(i*columnWidth, 0, columnWidth, size); 
     CGContextClipToRect(currentContext, clippedRect); 

     //create a rect equivalent to the full size of the image 
     CGRect drawRect = CGRectMake(0, 0, size, size); 

     //draw the image to our clipped context using our offset rect 
     CGContextDrawImage(currentContext, drawRect, image.CGImage); 
    } 

    //pull the image from our cropped context 
    UIImage *collage = UIGraphicsGetImageFromCurrentImageContext(); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 

    //Note: this is autoreleased 
    return collage; 
} 

Tôi đang làm gì sai?

PS hình ảnh cũng được vẽ lộn ngược.

Trả lời

9

CGContextClipToRect cắt hình chữ nhật cắt hiện tại bằng đối số được cung cấp. Vì vậy, lần thứ hai bạn gọi nó, bạn có hiệu quả biến vùng cắt của bạn thành không có gì.

Không có cách nào để khôi phục vùng cắt mà không khôi phục trạng thái đồ họa. Vì vậy, hãy gọi tới số CGContextSaveGState ở đầu vòng lặp của bạn và gọi tới số CGContextRestoreGState ở dưới cùng.

Phần lộn ngược có thể được sửa bằng cách điều chỉnh ma trận chuyển đổi hiện tại: gọi CGContextTranslateCTM để di chuyển xuất xứ và sau đó CGContextScaleCTM để lật trục y.

+1

' CGContextSaveGState' và 'CGContextRestoreGState' là những gì tôi đang tìm kiếm. Cảm ơn! –

0

Các lộn ngược hình ảnh xuống có thể được sửa chữa bằng cách gọi phương pháp dưới đây:

CGImageRef flip (CGImageRef im) { 
CGSize sz = CGSizeMake(CGImageGetWidth(im), CGImageGetHeight(im)); 
UIGraphicsBeginImageContextWithOptions(sz, NO, 0); 
CGContextDrawImage(UIGraphicsGetCurrentContext(), 
        CGRectMake(0, 0, sz.width, sz.height), im); 
CGImageRef result = [UIGraphicsGetImageFromCurrentImageContext() CGImage]; 
UIGraphicsEndImageContext(); 
return result; 

}

Hãy giúp đỡ từ bên dưới mã như đến nơi u sẽ đặt mã này:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(leftRect.size.width, leftRect.size.height), NO, 0); 
CGContextRef con = UIGraphicsGetCurrentContext(); 
CGContextDrawImage(con, leftRect,flip(leftReference)); 
Các vấn đề liên quan