开发者

UIImageView doesn't show up in UIScrollView

Hello I can't figure out why UIImageView doesn't want to show up in UIScrollView. Now here is a little catch, I am grabbing picture from library and adding i开发者_StackOverflow中文版t to the UIScrollView - that doesn't seem to work.

However if I just add picture right at "did load" method then it works just fine: [code]

UIImageView *tmp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"P3130016.JPG"]];

[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(800, 800)];
[scrollView addSubview:imageView];

I don't know why or where is the issue, please advise!


I'm slightly confused by your implementation, as you havn't posted all of your code, you are initilizing a 'tmp' imageview but i cannot see where it is used.

I'm going to persume you need to define the frame of the UIImageView after it has been initialized, currently the UIImageView has no frame and therefore is not drawn within the view.

You can set the frame like so:

UIImageView *tmp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"P3130016.JPG"]];

tmp.frame = CGRectMake(x,y,width,height);

These values need to be float values, where x and y are anchored from the top left of the screen. So CGRectMake(0,0,100,100) would draw a 100 by 100 square in the top left of the screen. You can find more information in the documentation here.

And then add it to the ScrollView:

[scrollView addSubview:tmp];

It is also worth noting that your *tmp UIImageView is currently leaking memory, i would suggest adding it to the autorelease pool on initialization:

UIImageView *tmp = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"P3130016.JPG"]] autorelease];


If you add an image to the scroll view after the scroll view has been loaded then you need to explicitly tell the view to redraw.

[scrollView setNeedsDisplay];

An iOS view draws itself as little as possible, so whenever something needing display changes you'll need to explicitly notify the view. See UIView setNeedsDisplay for more information.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜