【发布时间】:2013-12-18 08:53:29
【问题描述】:
我的主包中有 20 张图像,我想在我的滚动视图中水平显示它。
我做得很成功,但现在的问题是,在所有图像视图都设置在滚动视图中之后,图像在滚动视图中可见,这需要时间。
为了解决我创建了一个 nsoperation。
-(BOOL)sync:(NSError**)error
{
float imageLeftPosition = 0;
for (int imageCount = 1; imageCount<=syncObject.syncNumberOfImages; imageCount++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imageLeftPosition, 0, syncObject.syncImageWidth, syncObject.syncImageHeight)];
[imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d_%d.jpg",syncObject.syncChapterNumber,imageCount]]];
imgView.tag = imageCount;
// NSDictionary *dictionary = [NSDictionary new];
//
// [dictionary setValue:imgView forKey:CHAPTER_IMAGE];
imageLeftPosition = imageLeftPosition+syncObject.syncImageWidth;
//[dictionary setValue:[NSString stringWithFormat:@"%f",imageLeftPosition] forKey:CHAPTER_SCROLL_LEFT_POSITION];
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:imgView,[NSString stringWithFormat:@"%f",imageLeftPosition], nil] forKeys:[NSArray arrayWithObjects:CHAPTER_IMAGE,CHAPTER_SCROLL_LEFT_POSITION, nil]];
[self sendNotification:dictionary];
imgView = nil;
dictionary = nil;
}
return YES;
}
我在这里做的是添加创建一个图像视图并使用通知将其发布到主视图
-(void)addImageView:(NSNotification*)notification
{
NSInteger thread = [NSThread isMainThread]?CVC_MainThread:CVC_BackgroundThread;
switch (thread) {
case CVC_MainThread:
{
NSDictionary *dictionary = notification.userInfo;
if(dictionary != nil)
{
NSLog(@"image view : %@",(UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE]);
NSLog(@"leftposition: %f",[[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue]);
[_scrChapters addSubview:((UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE])];
_scrChapters.contentSize = CGSizeMake([[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue], 0);
}
dictionary = nil;
}
break;
case CVC_BackgroundThread:
{
dispatch_async(dispatch_get_main_queue(), ^(void) {
NSDictionary *dictionary = notification.userInfo;
if(dictionary != nil)
{
NSLog(@"image view : %@",(UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE]);
NSLog(@"leftposition: %f",[[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue]);
[_scrChapters addSubview:((UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE])];
_scrChapters.contentSize = CGSizeMake([[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue], 0);
}
dictionary = nil;
});
}
break;
default:
break;
}
}
这是通知处理程序方法,我在其中为主线程上的每个循环设置滚动视图中的内容大小和图像视图,在所有图像加载后仍然可见滚动视图。
我做错了什么?
【问题讨论】:
-
在此 [[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue] 中使用静态值作为图像计数*图像大小进行交叉检查,内容大小是否正确。
标签: ios objective-c ipad nsoperation nsoperationqueue