开发者

TTImageView problem

I want to display some TTImageView in a TTScrollView.

- (UIView*)scrollView:(TTScrollView*)scrollView pageAtIndex:(NSInteger)pageIndex {

TTView* pageView = nil;
if (!pageView) {
    pageView = [[[TTView alloc] init] autore开发者_如何学JAVAlease];
    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;
}

but there is a problem: TTImageView download image asynchronously. So it is quit possible that when code go to

 if (imageView.width > imageView.height){

this line the image is not loaded. So a exception is thrown.

Since the image view is added in

- (UIView*)scrollView:(TTScrollView*)scrollView pageAtIndex:(NSInteger)pageIndex {

so I can't move the below rotate code out of this method

  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;
   }

What I can do to solove this problem?


You can add a <TTImageViewDelegate> in your .h file and this function in the .m:

#pragma mark TTImageViewDelegate
-(void) imageView:(TTImageView *)imageView didLoadImage:(UIImage *)image{      
    if (image){
        //do your logic
    }
}

This function will be triggered when the image is finished loading.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜