UIImage 在开始时不适合 UIScrollView

发布于 2024-11-10 05:55:42 字数 951 浏览 3 评论 0原文

我有一个像这样的图像:

Want To Have

当我将图像加载到 uiimageview 然后作为子视图添加到 uiscrollview 时,开始图像显示如下:

Dont Want To Have

问题是我想在开始时看到所有适合屏幕的图像但它显示已经缩放。有没有办法处理这个问题请帮忙...

我有这样的代码:

[self.view addSubview:scrollView];
UIImageView *tempImageView = 
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];
self.imageView = tempImageView;
[tempImageView release];
[scrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];
scrollView.backgroundColor=[UIColor blackColor];
scrollView.contentSize = imageView.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / imageView.frame.size.width;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:1.0];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];

I have an image like:

Want To Have

When I load the image to uiimageview and then adding as a subview to uiscrollview, at start the image is showing like:

Dont Want To Have

The problem is I want to see all the image fit to screen at start but it is showing already zoomed. Is there a way to handle this please help ...

I have the code like:

[self.view addSubview:scrollView];
UIImageView *tempImageView = 
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];
self.imageView = tempImageView;
[tempImageView release];
[scrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];
scrollView.backgroundColor=[UIColor blackColor];
scrollView.contentSize = imageView.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / imageView.frame.size.width;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:1.0];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];

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

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

发布评论

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

评论(5

如果没有 2024-11-17 05:55:42

也许您正在寻找这个,

UIImageView *tempImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Watson.jpg"]] autorelease];
tempImageView.frame = scrollView.bounds;
self.imageView = tempImageView;

scrollView.backgroundColor = [UIColor blackColor];
scrollView.minimumZoomScale = 1.0  ;
scrollView.maximumZoomScale = imageView.image.size.width / scrollView.frame.size.width;
scrollView.zoomScale = 1.0;
scrollView.delegate = self;

[scrollView addSubview:imageView];

Probably you are looking for this,

UIImageView *tempImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Watson.jpg"]] autorelease];
tempImageView.frame = scrollView.bounds;
self.imageView = tempImageView;

scrollView.backgroundColor = [UIColor blackColor];
scrollView.minimumZoomScale = 1.0  ;
scrollView.maximumZoomScale = imageView.image.size.width / scrollView.frame.size.width;
scrollView.zoomScale = 1.0;
scrollView.delegate = self;

[scrollView addSubview:imageView];
乖乖兔^ω^ 2024-11-17 05:55:42

看一下UIView的contentMode-Property(UIImageView也支持这个)。

imageView.contentMode = UIViewContentModeScaleAspectFit;

这应该有效...

Have a look at the contentMode-Property of UIView (UIImageView also supports this).

imageView.contentMode = UIViewContentModeScaleAspectFit;

This should work...

离鸿 2024-11-17 05:55:42

我认为问题出在

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];

Apple 的文档中说“此方法调整接收器的框架以匹配指定图像的大小。默认情况下它还会禁用图像视图的用户交互。”

改为使用

UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tempImageView.image = [UIImage imageNamed:@"Tree.jpg"];

I think the problem is in

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];

Apple's docs say "This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default."

instead use

UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
tempImageView.image = [UIImage imageNamed:@"Tree.jpg"];
如果没有 2024-11-17 05:55:42

请注意,还有 [scrollView ZoomToRect:animated:] 描述 此处

您可以将其设置为图像的外边界,以便它会自动适应您的视图中的图像。但请确保按照上面的 Deepak 解决方案正确设置 MaximumZoomScale。

Note that there is also [scrollView zoomToRect:animated:] described here

You can set it to the outer bounds of your image so that it will auto-fit your image in your view. But make sure to setup correctly the maximumZoomScale as per Deepak solution above.

向地狱狂奔 2024-11-17 05:55:42

我使用 Deepak 的解决方案和 Apple 的建议,并将 UIScrollView 子类化,其中包含 UIImageView 。但是,我仅在滚动视图子类的第一个布局上缩放图像,并且仅当帧大小小于图像大小时:

- (void)layoutSubviews
{
[super layoutSubviews];

// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = _imageView.frame;

// center horizontally
if (frameToCenter.size.width < boundsSize.width)
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
    frameToCenter.origin.x = 0;

// center vertically
if (frameToCenter.size.height < boundsSize.height)
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
    frameToCenter.origin.y = 0;

_imageView.frame = frameToCenter;

// If image is bigger than frame, we zoom it on first load (zoom value is smaller than 1.0)
if (_firstLayout) {
    _firstLayout = NO;
    self.zoomScale = (self.frame.size.width < _imageView.image.size.width) ? (self.frame.size.width / _imageView.image.size.width) : 1.0;
}
}

这就是我初始化子类的方式:

- (id)initWithFrame:(CGRect)frame image:(UIImage*)image
{
self = [super initWithFrame:frame];
if (self) {

    _firstLayout = YES;

    _imageView = [[UIImageView alloc] initWithImage:image];
    [self addSubview:_imageView];

    self.backgroundColor = [UIColor blackColor];
    self.minimumZoomScale = 0.1;
    self.maximumZoomScale = 10;


    self.delegate = self;
}
return self;
}

I used Deepak's solution and Apples recommendations and subclassed UIScrollView with UIImageView inside it. However, I zoom the image only on first layout of my scrollview subclass and only if frame size is smaller than image size:

- (void)layoutSubviews
{
[super layoutSubviews];

// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = _imageView.frame;

// center horizontally
if (frameToCenter.size.width < boundsSize.width)
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
    frameToCenter.origin.x = 0;

// center vertically
if (frameToCenter.size.height < boundsSize.height)
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
    frameToCenter.origin.y = 0;

_imageView.frame = frameToCenter;

// If image is bigger than frame, we zoom it on first load (zoom value is smaller than 1.0)
if (_firstLayout) {
    _firstLayout = NO;
    self.zoomScale = (self.frame.size.width < _imageView.image.size.width) ? (self.frame.size.width / _imageView.image.size.width) : 1.0;
}
}

This is how I init my subclass:

- (id)initWithFrame:(CGRect)frame image:(UIImage*)image
{
self = [super initWithFrame:frame];
if (self) {

    _firstLayout = YES;

    _imageView = [[UIImageView alloc] initWithImage:image];
    [self addSubview:_imageView];

    self.backgroundColor = [UIColor blackColor];
    self.minimumZoomScale = 0.1;
    self.maximumZoomScale = 10;


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