UIImage Is Not Fitting In UIScrollView At Start

后端 未结 5 926
-上瘾入骨i
-上瘾入骨i 2020-12-30 12:07

I have an image like:

\"Want

When I load the image to uiimageview and then adding as a subvie

相关标签:
5条回答
  • 2020-12-30 12:27

    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"];
    
    0 讨论(0)
  • 2020-12-30 12:29

    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];
    
    0 讨论(0)
  • 2020-12-30 12:29

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

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    This should work...

    0 讨论(0)
  • 2020-12-30 12:31

    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.

    0 讨论(0)
  • 2020-12-30 12:34

    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;
    }
    
    0 讨论(0)
提交回复
热议问题