选择图像的一部分(剪辑)

发布于 2024-08-20 20:02:55 字数 444 浏览 4 评论 0原文

这可能不像我想象的那么难,但是如何选择图像的一部分?考虑以下示例:

替代文字

灰色区域是PNG或JPG格式的图像,现在我想从中选择红色的80x80像素区域。红色区域应该显示,其余区域不显示。我尝试了多种方法:

  • 将 UIImageView 的 ClipsToBounds 设置为 YES 并尝试偏移图像(不起作用)
  • 剪辑上部视图(不起作用)

我也查看了绘图功能,但还没有工作还没有这些,对我来说似乎有点牵强。有没有什么明显的方法可以做我想做但我错过的事情?我不想调整图像大小,只是剪裁。

感谢您抽出时间!

This is probably not as hard as I think it is, but how can I select part of an image? Consider the following example:

alt text

The grey area is an image in the PNG or JPG Format, now I want to select the red 80x80 px area from it. The red area should be displayed, the rest not. I have tried numerous approaches:

  • Make clipsToBounds of UIImageView to YES and the try to offset the image (doesn't work)
  • Clip the upper view (doesn't work)

I also had a look at the drawing functions, yet I have not worked with these yet and it seemed a little bit far fetched to me. Is there any obvious way for doing what I want to do that I am missing out on? I do not want the image to be resized, just clipped.

Thanks for your time!

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

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

发布评论

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

评论(4

小兔几 2024-08-27 20:02:55

这个问题中窃取以下代码并寻找 HitScan 的答案。

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);

Stole the code below from this question and look for HitScan's answer.

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);
花辞树 2024-08-27 20:02:55

如果您只想显示图像的中心,则可以通过 IB 将 UIImageView 设置内容模式为“Aspect Fill”。这将使它居中并剪掉任何多余的东西。

或者在代码中

myImageView.contentMode = UIViewContentModeScaleAspectFill;

If you only want to show the center of the image an a UIImageView set the content mode to "Aspect Fill" via IB. That will center it and crop off anything extra.

Or in code

myImageView.contentMode = UIViewContentModeScaleAspectFill;
偏爱自由 2024-08-27 20:02:55
myImageView.contentMode = UIViewContentModeCenter;
myImageView.clipsToBounds = YES;
myImageView.contentMode = UIViewContentModeCenter;
myImageView.clipsToBounds = YES;
柒七 2024-08-27 20:02:55

请参考下面的代码。在这里,我提供一个图像视图并将其分成两个相等的图像视图。

- (void)splitImageView:(UIImageView*)imgView{

UIImage *image = imgView.image;
CGRect rectForDrawing = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);

CGRect leftRect = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGRect rightRect = CGRectMake(imgView.frame.size.width/2, 0, imgView.frame.size.width/2, imgView.frame.size.height);


CGImageRef leftReference =  CGImageCreateWithImageInRect([image CGImage], leftRect);
CGImageRef rightReference = CGImageCreateWithImageInRect([image CGImage], rightRect);

UIGraphicsBeginImageContextWithOptions(rectForDrawing.size, NO, 0);
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con, rectForDrawing,flip(leftReference));
    imgViewLeft = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    imgViewLeft.frame = CGRectMake(imgView.frame.origin.x, imgView.frame.origin.y,
                               imgView.frame.size.width/2, imgView.frame.size.height);

    CGContextDrawImage(con, rectForDrawing,flip(rightReference));
    imgViewRight = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    imgViewRight.frame = CGRectMake(imgView.frame.origin.x+imgView.frame.size.width/2, imgView.frame.origin.y,
                                imgView.frame.size.width/2, imgView.frame.size.height);


UIGraphicsEndImageContext();


[self.view addSubview:imgViewLeft];
[self.view addSubview:imgViewRight]; 
}

Refer to the code below. Here i am providing an imageview and splitting the same in two equal imageviews.

- (void)splitImageView:(UIImageView*)imgView{

UIImage *image = imgView.image;
CGRect rectForDrawing = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);

CGRect leftRect = CGRectMake(0, 0, imgView.frame.size.width/2, imgView.frame.size.height);
CGRect rightRect = CGRectMake(imgView.frame.size.width/2, 0, imgView.frame.size.width/2, imgView.frame.size.height);


CGImageRef leftReference =  CGImageCreateWithImageInRect([image CGImage], leftRect);
CGImageRef rightReference = CGImageCreateWithImageInRect([image CGImage], rightRect);

UIGraphicsBeginImageContextWithOptions(rectForDrawing.size, NO, 0);
    CGContextRef con = UIGraphicsGetCurrentContext();
    CGContextDrawImage(con, rectForDrawing,flip(leftReference));
    imgViewLeft = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    imgViewLeft.frame = CGRectMake(imgView.frame.origin.x, imgView.frame.origin.y,
                               imgView.frame.size.width/2, imgView.frame.size.height);

    CGContextDrawImage(con, rectForDrawing,flip(rightReference));
    imgViewRight = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    imgViewRight.frame = CGRectMake(imgView.frame.origin.x+imgView.frame.size.width/2, imgView.frame.origin.y,
                                imgView.frame.size.width/2, imgView.frame.size.height);


UIGraphicsEndImageContext();


[self.view addSubview:imgViewLeft];
[self.view addSubview:imgViewRight]; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文