背景线程运行从Facebook相册中获取图像

时间:2011-12-20 09:48:31

标签: iphone facebook-graph-api nsthread

- (void)request:(FBRequest *)request didLoad:(id)result {


        // NSArray *resultData = [result objectForKey:@"data"];
        NSMutableArray *photos = [[NSMutableArray alloc] initWithCapacity:1];
        //  imageArray = [[NSMutableArray alloc] initWithCapacity:1];
        NSArray *resultData = [result objectForKey:@"data"];
        if ([resultData count] > 0) 
        {
            noOfImages = 0;

            for (NSUInteger i=0; i<[resultData count] ; i++) 
            {
                [photos addObject:[resultData objectAtIndex:i]];
                NSDictionary *albumPhotos = [photos objectAtIndex:i];
                NSString *imageUrl = [albumPhotos objectForKey:@"picture"];
                NSLog(@"ImageURL:%@",imageUrl);
                NSURL *url = [NSURL URLWithString:imageUrl];
               //  [slideImageArray addObject:url];
                NSData *data = [NSData dataWithContentsOfURL:url];
                UIImage *images = [UIImage imageWithData:data];
               // imageView.image = images;
                [slideImageArray addObject:images];

                [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob:) toTarget:self withObject:images];





            }

            NSLog(@"ImageCount:%d",[slideImageArray count]);  

            if([slideImageArray count] == 1)
            {
                imageView.image = [slideImageArray objectAtIndex:0];
            }

            if([slideImageArray count]>1)
            {
                numTimer =0;     
                myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerRunning) userInfo:nil repeats:YES];
            }



        }






}



- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
    NSLog(@"Err code: %d", [error code]);
  }

- (void)startTheBackgroundJob:(UIImage *)img 
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
    //imageView.image = img;
    noOfImages++;
    // [NSThread sleepForTimeInterval:3];
   // [slideImageArray addObject:img];

    //[NSThread sleepForTimeInterval:1];
     imageView.image = img;
    if(noOfImages == 1)
    {
        NSLog(@"noValueif:%d",noOfImages);
       // imageView.image = [slideImageArray objectAtIndex:0];
        [NSThread sleepForTimeInterval:1];
       // imageView.image = img;
    }

    else if(noOfImages >1)
    {
        NSLog(@"noValue:%d",noOfImages);
//        numTimer =0;     
//        myTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerRunning) userInfo:nil repeats:YES];
        // imageView.image = img;
        [NSThread sleepForTimeInterval:3];

    }



  //  [self performSelectorOnMainThread:@selector(changeImageViewImage) withObject:nil waitUntilDone:NO];
    [pool release];

}

我必须运行一个后台线程来获取Facebook相册中的图像。我必须先收集第一张图片来开始幻灯片放映。我睡了3秒但它没有用。还有其他任何方法用于从除NSData * data = [NSData dataWithContentsOfURL:url]之外的url获取图像;方法

1 个答案:

答案 0 :(得分:0)

你有没有找到这个问题的答案?或者你能解决它?

对于这个facebook api,您使用的是NSThread的个性化子类还是使用Dispatch队列?

要回答你的问题,如果你想在后台获取数据,请使用performSelectorONMainThread ...一些facebook SDK方法需要在主线程上运行才能使它们工作,根据我的个人经验,我发现大部分的使用此方法需要在主线程上请求数据。

它对我有用,希望它适合你 :)