我有一个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;
答案 0 :(得分:1)
在此代码区域中检查您要发送到scrollImageView的帧:
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];
您正在使用滚动条的框架放置对象,但滚动条的框架将是其PARENT视图中的IT坐标。因此,通过混合不同坐标的集合,您很可能将scrollImageView定位在滚动条的内容区域之外。
您可能希望使用scroller.bounds,这是滚动条内容区域的矩形。