UIImageView 未显示在 UIScrollView 内部
我有一个 iPhone 应用程序,应该在 UIScrollView 中显示图像。我认为我可以使用 Interface Builder 手动添加 UIScrollView,然后使用 UIImageView 添加图像。
我的做法是否错误?例如,我应该直接将图像添加到 UIScrollView 中吗?
这是我的代码:
- (void)request:(FBRequest *)request didLoad:(id)result {
//Code for array of photos
NSArray *photoAlbumArray=(NSArray*)[result valueForKey:@"data"];
arrayCount=[photoAlbumArray count];
//check to see if I did that correctly
[self.label setText:[NSString stringWithFormat:@"%i", arrayCount]];
//should have an array of photo objects and the number of objects, correct?
scrollWidth = 0;
scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);
for (int i = 0; i < arrayCount;i++) {
CGRect rect = scroller.frame;
rect.size.height = scroller.frame.size.height;
rect.size.width = scroller.frame.size.width;
rect.origin.x = scroller.frame.origin.x + scrollWidth;
rect.origin.y = scroller.frame.origin.y;
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:rect];
id individualPhoto = [photoAlbumArray objectAtIndex:i];
NSLog(@"%@",individualPhoto);
NSArray *keys=[individualPhoto allKeys];
NSLog(@"%@",keys);
NSString *imageURL=[individualPhoto objectForKey:@"source"];
//here you can use this imageURL to get image-data and display it in imageView
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//check to make sure the proper URL was passed
[self.label setText:imageURL];
scrollImageView.image = img;
//I have an imageView next to the UIScrollView to test whether that works - it does.
imageView.image = img;
[scroller addSubview:scrollImageView];
[img release];
[scrollImageView release];
scrollWidth += scroller.frame.size.width;
}
pageControl.numberOfPages=arrayCount;
pageControl.currentPage=0;
}
基于此调用:
-(IBAction)showAlbum:(UIButton *)sender
{
//Go goes here to get an album and display it in the UIScrollView
[_facebook requestWithGraphPath:@"ALBUM_ID/photos" andDelegate:self];
}
我在 UIScrollView 左侧的 UIImageView 中显示图像(用于测试目的),并且 UIImageView 工作得很好。所以这就是为什么我想知道我是否在处理这个错误,或者我只是在编码中犯了错误。
你能帮忙吗?因为过去几个小时我一直在研究这个问题,但还没有取得任何进展。
更新 - 这是我根据克里斯的建议所做的。它对我有用,但如果仍然有任何问题,请指出。
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:[scroller bounds]];
CGRect rect = scrollImageView.frame;
rect.origin.x = scrollImageView.frame.origin.x + scrollWidth;
rect.origin.y = scrollImageView.frame.origin.y;
scrollImageView.frame = rect;
I have an iPhone application that is supposed to show images inside a UIScrollView. I thought that I could add the UIScrollView manually using Interface Builder and then add images using UIImageView.
Am I approaching this incorrectly? For example, should I just be adding images to the UIScrollView directly?
Here's my code:
- (void)request:(FBRequest *)request didLoad:(id)result {
//Code for array of photos
NSArray *photoAlbumArray=(NSArray*)[result valueForKey:@"data"];
arrayCount=[photoAlbumArray count];
//check to see if I did that correctly
[self.label setText:[NSString stringWithFormat:@"%i", arrayCount]];
//should have an array of photo objects and the number of objects, correct?
scrollWidth = 0;
scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);
for (int i = 0; i < arrayCount;i++) {
CGRect rect = scroller.frame;
rect.size.height = scroller.frame.size.height;
rect.size.width = scroller.frame.size.width;
rect.origin.x = scroller.frame.origin.x + scrollWidth;
rect.origin.y = scroller.frame.origin.y;
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:rect];
id individualPhoto = [photoAlbumArray objectAtIndex:i];
NSLog(@"%@",individualPhoto);
NSArray *keys=[individualPhoto allKeys];
NSLog(@"%@",keys);
NSString *imageURL=[individualPhoto objectForKey:@"source"];
//here you can use this imageURL to get image-data and display it in imageView
NSURL *url = [NSURL URLWithString:imageURL];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
//check to make sure the proper URL was passed
[self.label setText:imageURL];
scrollImageView.image = img;
//I have an imageView next to the UIScrollView to test whether that works - it does.
imageView.image = img;
[scroller addSubview:scrollImageView];
[img release];
[scrollImageView release];
scrollWidth += scroller.frame.size.width;
}
pageControl.numberOfPages=arrayCount;
pageControl.currentPage=0;
}
Based on this call:
-(IBAction)showAlbum:(UIButton *)sender
{
//Go goes here to get an album and display it in the UIScrollView
[_facebook requestWithGraphPath:@"ALBUM_ID/photos" andDelegate:self];
}
I'm displaying the image in a UIImageView to the left of the UIScrollView (for testing purposes) and the UIImageView works just fine. So that's why I'm wondering if I'm approaching this wrong or if I just made a mistake in my coding.
Could you please help? Because I've been working on this for the past few hours and haven't gotten any further.
UPDATE - here is what I did based on Chris' recommendation. it works for me but if there are any issues still please do point them out.
UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:[scroller bounds]];
CGRect rect = scrollImageView.frame;
rect.origin.x = scrollImageView.frame.origin.x + scrollWidth;
rect.origin.y = scrollImageView.frame.origin.y;
scrollImageView.frame = rect;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查您发送到scrollImageView的框架,在这个代码区域:
您正在使用滚动条的框架来放置对象,但滚动条的框架将是它在其父视图中的坐标。因此,通过混合不同的坐标集,您很可能将scrollImageView定位在滚动条的内容区域之外。
您可能想改用scroller.bounds,它是滚动条内容区域的矩形。
Check the frame you're sending to scrollImageView, in this area of code:
You're using the scroller's frame to place the object, but the scroller's frame will be IT'S coordinates in it's PARENT view. So by mixing sets of different coordinates, you're most likely positioning the scrollImageView outside the scroller's content area.
You probably want to use scroller.bounds instead, which is the rectangle for the scroller's content area.