gpt4 book ai didi

ios - 在 UIScrollView 问题中裁剪缩放图像

转载 作者:行者123 更新时间:2023-11-28 18:03:10 25 4
gpt4 key购买 nike

经过this link之后,我的代码的问题是输出图像无法设置正确的 x 和 y 值,因为裁剪后的图像在结果图像中似乎有 0 和 0,无论我缩放到哪里(或计算滚动偏移的位置)。这是我试过的。

- (IBAction)crop:(id)sender
{

float zoomScale = 1.0f / [self.scroll zoomScale];

CGRect rect;
NSLog(@"contentOffset is :%f,%f",[self.scroll contentOffset].x,[self.scroll contentOffset].y);
rect.origin.x = self.scroll.contentOffset.x * zoomScale;
rect.origin.y = self.scroll.contentOffset.y * zoomScale;
rect.size.width = self.scroll.bounds.size.width * zoomScale;
rect.size.height = self.scroll.bounds.size.height * zoomScale;

UIGraphicsBeginImageContextWithOptions( CGSizeMake(rect.size.width, rect.size.height),
NO,
0.);

NSLog(@"rect offset is :%f,%f",rect.origin.x,rect.origin.y);
CGPoint point = CGPointMake(-rect.origin.x, -rect.origin.y); **//even though above NSLog have some values, but output image is unable to set proper x and y values as cropped image seems to have 0 and 0 in the resultant image.**
[[self.imagV image] drawAtPoint:point
blendMode:kCGBlendModeCopy
alpha:1];
self.croppedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

DTImageViewController *imageViewController = [[DTImageViewController alloc] initWithNibName:@"DTImageViewController" bundle:nil];
imageViewController.image = self.croppedImage;
[self.navigationController pushViewController:imageViewController animated:YES];
}

最佳答案

与已经发布的代码类似,但只是在不传递 CGRect 的情况下采用整个 UIScrollView 边界

-(void)takeScreenShotOfScrollView
{
UIGraphicsBeginImageContextWithOptions(scrollView.bounds.size, YES, [UIScreen mainScreen].scale);
CGPoint offset = scrollView.contentOffset;
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y);

[scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

img = [SDImageHelper imageWithImage:img_image scaledToSize:CGSizeMake(769, 495)];
}

奖励:

裁剪方法

+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

关于ios - 在 UIScrollView 问题中裁剪缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25015550/

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