我目前正在动态地将 UIImage 添加到我的 UIScrollView 对象中,并且一切都按预期工作。我现在需要添加额外的功能(从互联网加载时图像中心的 UIActivityIndicator 和下方的标签)所以我想为什么不创建一个自定义视图,其中包含我需要的所有这些布局是,然后将其加载到滚动视图中。但是我遇到了一个问题,当我将它添加到scrollView时,什么都没有出现。这是我所拥有的:
新闻视图控制器.m:
imageScrollView.contentSize = CGSizeMake(320*numberOfPages, 303);
pageControl.numberOfPages = numberOfPages;
dispatch_queue_t imageDownload = dispatch_queue_create("imageDownload", NULL);
__block NSData *temp;
CustomImageView *customImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 303)];
[imageScrollView addSubview:customImageView];
[[customImageView activityIndicator] startAnimating];
dispatch_async(imageDownload, ^{
temp = [[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.wlfarm.org/wp-content/uploads/2012/05/farm.jpg"]]retain];
dispatch_async(dispatch_get_main_queue(), ^{
customImageView.imageView.image = [[UIImage alloc] initWithData:temp];
[[customImageView activityIndicator] stopAnimating];
[customImageView setNeedsDisplay];
customImageView.caption.text = @"HAHAHAHAHHAHA";
[imageScrollView setNeedsDisplay];
[temp release];
});
});
dispatch_release(imageDownload);
CustomImageView.h:
#import <UIKit/UIKit.h>
@interface CustomImageView : UIView
{
IBOutlet UIImageView *imageView;
IBOutlet UILabel *caption;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *caption;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
@end
自定义图像视图.m
#import "CustomImageView.h"
@interface CustomImageView ()
@end
@implementation CustomImageView
@synthesize caption, imageView, activityIndicator;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
我包括我的 XIB 文件的屏幕截图,以及在模拟器上运行的程序。第一张图片在滚动视图中没有显示任何内容(使用我的自定义类进行的尝试),滚动视图的第二页是通过将 UIImageView 添加到 UIScrollView 进行的尝试(有效)。谁能指出我做错了什么?我不允许将自定义视图加载到 UIScrollView 中吗?谢谢你的帮助!
IB 截图 - http://i.stack.imgur.com/gz9UL.png
iOS 模拟器没有带有 CustomView 的图像屏幕截图 - http://i.stack.imgur.com/zhswq.png
带有 UIImageView 屏幕截图的 iOS 模拟器图像 - http://i.stack.imgur.com/97vmU.png