我想在TTImageView
中显示一些TTScrollView
。
- (UIView*)scrollView:(TTScrollView*)scrollView pageAtIndex:(NSInteger)pageIndex {
TTView* pageView = nil;
if (!pageView) {
pageView = [[[TTView alloc] init] autorelease];
pageView.backgroundColor = [UIColor clearColor];
pageView.userInteractionEnabled = NO;
}
NSString *imagePath = [self.screenShotImgPath objectAtIndex:pageIndex];
TTImageView *imageView = [[[TTImageView alloc] initWithFrame:CGRectZero] autorelease];
imageView.autoresizesToImage = YES;
imageView.urlPath = imagePath;
imageView.backgroundColor = [UIColor clearColor];
imageView.delegate = self;
//here I need to rotate image
if (imageView.width > imageView.height){
CGAffineTransform tran = CGAffineTransformIdentity;
CGSize bnds = imageView.size;
CGSize bnds2 = imageView.size;
bnds = [self swapWidthAndHeight:bnds];
tran = CGAffineTransformMakeTranslation(0.0,bnds2.width);
tran = CGAffineTransformRotate(tran, [self degreesToRadians:-90.0]);
imageView.transform = tran;
}
//here I need to scale image view frame to fit image actual scale
CGFloat scale = imageView.width/imageView.height;
CGFloat scaledWidth = (scrollView.height - 20.f) * scale;
imageView.frame = CGRectMake((scrollView.width - scaledWidth)/2, 10.f, scaledWidth, scrollView.height - 20.f);
[pageView addSubview:imageView];
return pageView;
}
但是有一个问题:TTImageView
异步下载图像。因此,当代码转到
if (imageView.width > imageView.height){
此行未加载图像。因此抛出异常。
由于图像视图已添加到
中- (UIView*)scrollView:(TTScrollView*)scrollView pageAtIndex:(NSInteger)pageIndex {
所以我无法将以下旋转代码移出此方法
if (imageView.width > imageView.height){
CGAffineTransform tran = CGAffineTransformIdentity;
CGSize bnds = imageView.size;
CGSize bnds2 = imageView.size;
bnds = [self swapWidthAndHeight:bnds];
tran = CGAffineTransformMakeTranslation(0.0,bnds2.width);
tran = CGAffineTransformRotate(tran, [self degreesToRadians:-90.0]);
imageView.transform = tran;
}
我能做些什么来解决这个问题?
答案 0 :(得分:3)
您可以在.h文件中添加<TTImageViewDelegate>
这个函数在.m:
#pragma mark TTImageViewDelegate
-(void) imageView:(TTImageView *)imageView didLoadImage:(UIImage *)image{
if (image){
//do your logic
}
}
图像加载完成后将触发此功能。