我有20個圖像在我的主包,我想顯示它在我的滾動視圖水平。ScrollView未更新imageview
我成功地做到了,但現在問題是所有imageview都設置在滾動視圖中後,滾動視圖中可見圖像,並且需要時間。
爲了解決這個問題,我創建了一個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;
}
}
這是通知處理方法,在這裏我設置的內容大小和圖像視圖中滾動在加載所有圖像後,主線程靜態滾動視圖上的每個循環的視圖都可見。
我做錯了什麼?
是內容大小是在該正確[[字典valueForKey:CHAPTER_SCROLL_LEFT_POSITION]的floatValue]使用靜態值作爲圖像數*圖像尺寸交叉檢查。 –