如何在 uiimageview/uiscrollview 中将图像居中(缩放前和缩放后)
我试图将图像居中(相对于 iPhone 屏幕),以便在加载视图时,图像的宽度/高度为中心...在我的情况下,我的视图加载在图像本身的像素点 0,0 处,如果我缩小图像,图像会出现在左上角,而不是屏幕/视图的中间位置。这是我到目前为止所得到的:
UIImage *image = [UIImage imageNamed: @"t.bmp"];
self.view.backgroundColor = [UIColor blackColor];
self.mi.frame = CGRectMake(0, 0, image.size.width, image.size.height);
self.mi.contentMode = (UIViewContentModeCenter);
self.mi.backgroundColor = [UIColor blackColor];
[self.mi setImage:image];
/* Draw a line here!!
UIGraphicsBeginImageContext(self.mi.frame.size);
[self.mi.image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 20.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0,0);
CGContextAddLineToPoint(context, 200, 200);
CGContextMoveToPoint(context, 200,0);
CGContextAddLineToPoint(context, 0, 200);
CGContextStrokePath(context);
self.mi.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
*/
self.expimg.maximumZoomScale = 2.0;
self.expimg.minimumZoomScale = 0.1;
self.expimg.clipsToBounds = YES;
self.expimg.delegate = self;
[self.expimg setContentSize:self.mi.frame.size];
[self.expimg addSubview: self.mi];
[self.view addSubview: self.expimg];
谢谢!
编辑:mi 是一个 UIImageView,expimg 是一个 UIScrollView(使用的界面生成器)。另外,还定义了 viewForZoomingInScrollView。
I am trying to center an image (with respect to the iphone screen) such that when the view is loaded the image is center width/height... In my case my view is loaded at pixel points 0,0 of the image itself and if I zoom out the image appears in the top left corner as opposed to mid-center of the screen/view. Here is what I have so far:
UIImage *image = [UIImage imageNamed: @"t.bmp"];
self.view.backgroundColor = [UIColor blackColor];
self.mi.frame = CGRectMake(0, 0, image.size.width, image.size.height);
self.mi.contentMode = (UIViewContentModeCenter);
self.mi.backgroundColor = [UIColor blackColor];
[self.mi setImage:image];
/* Draw a line here!!
UIGraphicsBeginImageContext(self.mi.frame.size);
[self.mi.image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 20.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0,0);
CGContextAddLineToPoint(context, 200, 200);
CGContextMoveToPoint(context, 200,0);
CGContextAddLineToPoint(context, 0, 200);
CGContextStrokePath(context);
self.mi.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
*/
self.expimg.maximumZoomScale = 2.0;
self.expimg.minimumZoomScale = 0.1;
self.expimg.clipsToBounds = YES;
self.expimg.delegate = self;
[self.expimg setContentSize:self.mi.frame.size];
[self.expimg addSubview: self.mi];
[self.view addSubview: self.expimg];
Thanks!
Edit: mi is a UIImageView and expimg is a UIScrollView (used interface builder). Also, viewForZoomingInScrollView is defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够通过在初始加载时以及缩放级别发生变化时调整图像和滚动视图的位置和大小来实现此目的。创建一个在
viewDidLoad
上调用的方法,并在UIScrollViewDelegate
方法scrollViewDidZoom:
上再次调用。像这样的东西应该可以工作(其中imageView和scrollView是类属性):
我还没有测试过这个,所以我的一些数学可能是错误的。无论哪种方式,它都希望能让您很好地了解该怎么做。
You should be able to do this by adjusting the position and size of the image and scroll view on initial load, and as zoom levels change. Make a method that is called on
viewDidLoad
, and again on theUIScrollViewDelegate
methodscrollViewDidZoom:
.Something like this should work (where imageView and scrollView are class attributes):
I have not tested this, so some of my math could be wrong. Either way, it will hopefully give you a good idea of what to do.
第 1 步:创建一个图像视图属性,然后在 viewDidLoad 处初始化它并添加到滚动视图:
第 2 步:创建 adjustZoomScale 函数以获得最小比例
第 3 步:因为滚动框架视图将在 viewDidAppear 处更新,因此我们需要在此处调整缩放比例。并设置 alpha 使图像看起来更平滑:
步骤 4:实现 viewForZoomingInScrollView (滚动委托方法)并返回图像视图
步骤 5:实现 scrollViewDidZoom (滚动委托方法)并调整点查看图片
感谢 shawhu 这篇文章用于调整点缩放时的图像视图
Step 1: You create an image view property then init it at viewDidLoad and add to scrollView:
Step 2: Create adjustZoomScale function to get minimum scale
Step 3: Because frame of scroll view will be updated at viewDidAppear so we need to adjust zoom scale here. And set alpha to make image appear smoother:
Step 4: Implement viewForZoomingInScrollView (scroll delegate method) and return image view
Step 5: Implement scrollViewDidZoom (scroll delegate method) and adjust point for image view
Thank shawhu at this post for point adjusting for image view while zooming
[scrollView ZoomToRect:CGRectMake(x, y, 宽度, 高度) 动画:YES];将为你工作...
其中 rect 应该以这些中心坐标为原点。
[scrollView zoomToRect:CGRectMake(x, y, width, height) animated:YES]; will work for you...
Where rect should have those center coordinates as origin.