ScrollView未使用imageview进行更新

时间:2013-12-18 08:53:29

标签: ios objective-c ipad nsoperation nsoperationqueue

我的主要包中有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;
    }
}

这是通知处理程序方法,其中我在滚动视图中为主线程上的每个循环设置内容大小和图像视图仍然可以在加载所有图像后看到滚动视图。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

尝试使用pagedflowview。这将仅将可见项加载到Scrollview中,并且易于自定义。您也可以搜索iCarousel。

https://www.cocoacontrols.com/controls/pagedflowview

答案 1 :(得分:0)

试试这个

-(void)showimagesasSlide:(int) totalimagesCount
{
    int xAxisofimageView=0;

    for (int initialimagesCount=0;initialimagesCount<totalimagesCount; initialimagesCount++) {
        UIImageView *detailedImageview=[[UIImageView alloc] init];
        detailedImageview.userInteractionEnabled=YES;

        UIImage *dynamicImage=[ScaleImageSize squareImageWithImage:[images objectAtIndex:initialimagesCount] scaledToSize:CGSizeMake(300, 500) ];


        if (IS_IPHONE_5) {
            detailedImageview.frame=CGRectMake(xAxisofimageView, 0, 320, 568);

        }
        else
        {
            detailedImageview.frame=CGRectMake(xAxisofimageView, 0, 320, 460);

        }
     //   detailedImageview.contentMode=UIViewContentModeCenter;


        detailedImageview.backgroundColor=[UIColor clearColor];
        detailedImageview.image=dynamicImage;
        detailedImageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin  ;
        detailedImageview.tag=initialimagesCount+12;

        [mainScrollview addSubview:detailedImageview];
        xAxisofimageView=xAxisofimageView+320;

      }
    mainScrollview.contentSize=CGSizeMake(320*images.count, 0);
}