gpt4 book ai didi

ios - CGRect 裁剪图像显示错误值

转载 作者:行者123 更新时间:2023-11-28 22:05:39 28 4
gpt4 key购买 nike

当我尝试裁剪图像的底部时,结果从左上角放大。没有裁剪,图像显示得很好。我真的看不出我在裁剪功能上做错了什么。为什么这不返回带有裁剪的完整图像?请参阅下面的代码和结果:

- (UIImage *)croppedImageFromScreenShot
{
// Prepare view for screen capture

// Screen Capture

CGRect fullScreenRect = self.view.bounds;

UIGraphicsBeginImageContextWithOptions(fullScreenRect.size, NO, [UIScreen mainScreen].scale);

[self.view drawViewHierarchyInRect:fullScreenRect afterScreenUpdates:YES];

UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Crop Image

CGFloat cropDistance = 20.0;

CGSize imageSize = screenShotImage.size;

CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);

CGRect cropRect = CGRectInset(imageRect, 0.0, cropDistance);

CGImageRef imageRef = CGImageCreateWithImageInRect([screenShotImage CGImage], cropRect);

UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

// Restore view after screen capture


return croppedImage;
}

裁剪效果不错,但图像放大变小了:

enter image description here

在没有裁剪的情况下,从下面的代码中大小就可以了:

- (UIImage *)croppedImageFromScreenShot
{
// Prepare view for screen capture

// Screen Capture

CGRect fullScreenRect = self.view.bounds;

UIGraphicsBeginImageContextWithOptions(fullScreenRect.size, NO, [UIScreen mainScreen].scale);

[self.view drawViewHierarchyInRect:fullScreenRect afterScreenUpdates:YES];

UIImage *screenShotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


return screenShotImage;
}

返回原始图像,因为它也应该使用裁剪功能:

enter image description here

最佳答案

改变这个:

CGRect cropRect = CGRectInset(imageRect, 0.0, cropDistance);

进入:

CGRect cropRect = CGRectMake(0,0,imageSize.width, imageSize.height - cropDistance);

你只想定义你想要的特定矩形作为图像的子集,所以如果你想裁剪底部,那么你只需从高度中减去。

关于ios - CGRect 裁剪图像显示错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24088553/

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