【发布时间】:2014-01-15 14:23:15
【问题描述】:
我正在尝试设置一个 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;
}
在加载视图控制器时,只需完成以下操作:
_scrollView.contentSize=self.view.frame.size;
[_scrollView setImage:[UIImage imageNamed:@"WeCanDoIt"]];
我遇到的问题是图像实际上并没有正确加载/加载。即使在 Image 视图上的 contentMode 是在创建时设置的,但最初的图像显示是这样的。
任何人都可以帮助解释为什么这可能无法按预期工作吗?
这是完整的截图:
底部应该有一些空白(这是实际图像)但不在屏幕上。
【问题讨论】:
标签: ios ios7 uiscrollview uiimageview uiimage