如何使用UIImageView显示大尺寸图像

时间:2017-08-26 21:37:33

标签: ios objective-c uiscrollview uiimageview uiimage

在我的应用程序中,我有一个800 x 2000的大尺寸图像。我想在UIImageView中显示这个图像,这个图像也是可滚动的。

我试过以下代码。

UIImage *img=[UIImage imageNamed:@"About_CVR_Detail"];
self.aImgView.frame=CGRectMake(0,0, Screen_Width,img.size.height);
self.aImgView.image=img;
self.aScrollView.contentSize=CGSizeMake(Screen_Width, self.aImgView.frame.size.height);

而且这个也是:

self.aScrollView.backgroundColor=[UIColor colorWithPatternImage:[UIImage  imageNamed:@"About_CVR_Detail"]];

提前致谢

2 个答案:

答案 0 :(得分:1)

如果你想在水平和垂直方向上滚动图像,你可以像danh所说的那样。

但是如果你只想要一个滚动,你应该设置固定的宽度或高度,并保持比例,也应该计算另一个长度。

例如:

UIImage *img=[UIImage imageNamed:@"image.png"];
self.aImgView.frame=CGRectMake(0,0, Screen_Width, Screen_Width * img.size.height / img.size.width);
self.aImgView.image=img;

[self.aScrollView addSubview:self.aImgView];
self.aScrollView.contentSize= self.aImgView.frame.size;

上面的代码只允许您在垂直方向上滚动。

答案 1 :(得分:0)

请记住将图像视图添加为滚动视图的子视图。此外,您可以使用图像的大小(在initWithImage中设置图像视图,其边界可用于滚动视图contentSize)来简化布局。

UIImage *img=[UIImage imageNamed:@"About_CVR_Detail"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];

[self.aScrollView addSubview:imageView];
self.aScrollView.contentSize = imageView.bounds.size;