gpt4 book ai didi

iphone - 如何以编程方式将图像裁剪成碎片

转载 作者:行者123 更新时间:2023-12-03 18:34:31 25 4
gpt4 key购买 nike

我需要以编程方式将图像分为 9 block 。关于如何执行此操作有什么建议吗?

最佳答案

下面的代码也是一个检测被点击的图片片段的解决方案。这个想法是获取一个 UIImage 并使用 CGImageCreateWithImageInRect 来裁剪部分。从裁剪的片段中创建一个新的 UIImage 并将其放置在 UIImageView 中。为了让点击手势正常工作,我必须将 UIImageView 放置在 UIView 中。最后,提供手势和唯一标签,以便在点击时可以识别该作品。

- (void)loadView {
UIView* root = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
UIImage* whole = [UIImage imageNamed:@"whole.jpg"]; //I know this image is 300x300

int partId = 0;
for (int x=0; x<=200; x+=100) {
for(int y=0; y<=200; y+=100) {
CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, 100, 100));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* iv = [[UIImageView alloc] initWithImage:part];

UIView* sView = [[UIView alloc] initWithFrame:CGRectMake(200-x, 200-y, 100, 100)];
[sView addSubview:iv];
[iv release];

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tap:)];
tap.numberOfTapsRequired = 1;
[sView addGestureRecognizer:tap];
[tap release];

sView.tag = partId;

[root addSubview:sView];
[sView release];
partId++;
CGImageRelease(cgImg);
}
}

self.view = root;
}

- (void)tap:(UITapGestureRecognizer*)gesture
{
NSLog(@"image tap=%d", gesture.view.tag);
}

关于iphone - 如何以编程方式将图像裁剪成碎片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4743242/

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