使用UIImageView设置缩放UIScrollView

时间:2013-12-28 01:34:14

标签: ios ios7 uiscrollview uiimageview uiimage

我正在尝试设置一个UIScrollView子类,在其中加载UIImageView和UIImage。图片应该是可缩放的(通过捏合)遵循这里的苹果文档 - https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html

我已经创建了一个UIScrollView的子类,它完成了以下内容:

@interface SPMScrollView () {}

@property (nonatomic) float zoomedInScale;

@property (nonatomic) float naturalZoomScale;

@end

@implementation SPMScrollView

-(id)initWithCoder:(NSCoder *)aDecoder {
    NSLog(@"%s",__PRETTY_FUNCTION__);
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor blackColor];

        super.delegate = self;

        self.imageView = [[UIImageView alloc] initWithFrame:self.frame];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.imageView];

    }
    return self;
}


-(void)setupScales {
    self.maximumZoomScale = 2.0f;
    self.minimumZoomScale = 1.0f;
}


-(void)setImage:(UIImage *)image {

        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image = image;
            self.imageView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
            [self setupScales];
        });

    NSLog(@"The image frame is: %@",NSStringFromCGRect(self.imageView.frame));

}

#pragma mark - UIScrollViewDelegate methods
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

在加载时,View Controller只需完成以下操作:

_scrollView.contentSize=self.view.frame.size;
[_scrollView setImage:[UIImage imageNamed:@"WeCanDoIt"]];

我遇到的问题是图像实际上没有正确加载到第一个位置/加载。即使想到图像视图中的contentMode在创建时被设置,图像最初也会显示出来。

enter image description here

有人可以协助解决为什么这可能无法正常工作吗?

这是一个完整的截图:
enter image description here
底部应该有一些空白区域(这是实际图像),但是屏幕外。

2 个答案:

答案 0 :(得分:2)

问题仅在于IOS7。问题是origin.y设置不正确,中心不正确。

经过一段时间的测试后,我发现问题是由于创建了自动滚动插件:

if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
    self.automaticallyAdjustsScrollViewInsets = NO;
}

以上解决了我的问题

答案 1 :(得分:0)

你将contentMode设置为UIViewContentModeScaleAspectFit如果UIImageView框架的比例与图像不同,那么在上部和底部应该有一些黑色边框。您可以设置UIViewContentModeScaleToFill以查看UIImageView是否正确,但这次图像不是按比例拉伸。