在核心图形中创建拼贴的多个剪切矩形

发布于 2024-08-21 12:03:33 字数 1931 浏览 5 评论 0原文

我正在创建一个与其他图像元素相结合的拼贴画。这里有一些 ASCII 艺术来解释我正在做的事情:

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.

我写了一些代码,但它只显示第一列而不是其余的......我想我必须以某种方式清除剪辑,bt 我不知道该怎么做或者这是否是最好的方法。

+ (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;
}

我做错了什么?

PS,图像也是颠倒绘制的。

I am creating a collage combined with elements from other images. Here is some ASCII art to explain what I am doing:

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.

I have some code written but it is only showing the first column and not the rest… I think I have to clear the clipping somehow, bt I am not sure of how to do it or whether this is even the best approach.

+ (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;
}

What am I doing wrong?

PS the image is drawing upside down too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

緦唸λ蓇 2024-08-28 12:03:33

CGContextClipToRect 使当前剪切矩形与提供的参数相交。因此,当您第二次调用它时,您实际上将剪切区域变为空。

如果不恢复图形状态,就无法恢复剪切区域。因此,请在循环顶部调用 CGContextSaveGState,并在循环底部调用 CGContextRestoreGState

上下颠倒的部分可以通过调整当前变换矩阵来修复:调用CGContextTranslateCTM移动原点,然后调用CGContextScaleCTM翻转y轴。

CGContextClipToRect intersects the current clipping rectangle with the argument provided. So the second time you call it, you are effectively turning your clipping region to nothing.

There is no way to restore the clipping region without restoring the graphics state. So, make a call to CGContextSaveGState at the top of your loop and a call to CGContextRestoreGState at the bottom.

The upside-down part can be fixed by adjusting the current transformation matrix: call CGContextTranslateCTM to move the origin and then CGContextScaleCTM to flip the y-axis.

夜声 2024-08-28 12:03:33

可以通过调用以下方法来纠正颠倒的图像:

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;

}

从下面的代码中获取有关您将在何处放置此代码的帮助:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(leftRect.size.width, leftRect.size.height), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, leftRect,flip(leftReference));

The upside down image can be corrected by calling the below method :

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;

}

Take help from below code as to where u will put this code:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(leftRect.size.width, leftRect.size.height), NO, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
CGContextDrawImage(con, leftRect,flip(leftReference));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文