gpt4 book ai didi

ios - 我如何以编程方式将一些 UIImage 组合在一起以获得一个大的 UIImage?

转载 作者:行者123 更新时间:2023-11-28 19:02:25 27 4
gpt4 key购买 nike

这是我的代码:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{

// at this point the webView scrolled to the next section

// I save the offset to make the code a little easier to read
CGFloat offset = _webPage.scrollView.contentOffset.y;

UIGraphicsBeginImageContextWithOptions(_webPage.bounds.size, NO, 0);

[_webPage.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

// if we are not done yet, scroll to next section
if (offset < _webPage.scrollView.contentSize.height)
{
[_webPage.scrollView scrollRectToVisible:CGRectMake(0, _webPage.frame.size.height+offset, _webPage.frame.size.width, _webPage.frame.size.height) animated:YES];
}
}

我在其中通过滚动 web view 保存了未定义数量的屏幕截图 (UIImages)。这行得通,我的照片库中有网页的所有部分。但我不想要零件,我想要一个长的 UIImage。那么如何将我的 UIImage 放在一起(一个接一个?)?

最佳答案

你可以写一个 UIImage 类别来做到这一点

UIImage+Combine.h

#import <UIKit/UIKit.h>

@interface UIImage (Combine)

+ (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage;

@end

UIImage+Combine.m

#import "UIImage+Combine.h"


@implementation UIImage (Combine)

+ (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {
UIImage *image = nil;

CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), firstImage.size.height + secondImage.size.height);
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]);
} else {
UIGraphicsBeginImageContext(newImageSize);
}
[firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2), 0)];
[secondImage drawAtPoint:CGPointMake(roundf(((newImageSize.width-secondImage.size.width)/2) ),
roundf((newImageSize.height-secondImage.size.height)))];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}

然后您可以在您的代码中调用该函数:

UIImage *img = [UIImage imageByCombiningImage:image1 withImage:image2];

这将绘制一个新图像,该图像具有两个图像中最大的宽度和两个图像组合的高度。 image1 将位于顶部,image2 位于其下方。

关于ios - 我如何以编程方式将一些 UIImage 组合在一起以获得一个大的 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23649182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com