开发者

TTImageView sizeToFit makes frame size zero but still displays image

I am using a TTImageView (from three20) to display an image from the web.

self.pic = [[TTImageView alloc] initWithFrame:CGRectMake(25, 25, 100, 100)]; 
self.pic.urlPath = @"http://www.google.com/images/logos/ps_logo2.png";
[self.pic sizeToFit];
NSLog(@"%f %f %f %f", self.pic.frame.origin.x, self.pic.frame.origin.y, self.pic.frame.size.width, self.pic.frame.size.height);
开发者_如何学JAVA

the image loads and displays perfectly but the NSLog returns "25.000000 25.000000 0.000000 0.000000". How do I get this to return the correct frame?


It's been a while since I used TTImageView, but what I imagine is happening is the following:

  1. You ask TTImageView to load a new image from a URL
  2. Immediately after you set the URL path, you call sizeTo Fit
  3. ...but the image actually hasn't been downloaded yet, so the frame is set to zero because no image exists at that time

TTImageView will download asynchronously - that is, in the background. You need to use the delegate function TTImageViewDelegate (documented here) to get a call back when the image has finished loading, at which point you should call sizeToFit.

Currently, you're firing off a request to download the image - a process that could take several seconds on 3G, and still a fair amount of time on WiFi- and then the next line asks to resize the frame. You need to call sizeToFit after you're sure the image download has finished, which is what the delegate methods are for.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜