iphone – 如何使用多线程在UITableView中显示json图像数组?

iphone – 如何使用多线程在UITableView中显示json图像数组?,第1张

概述我的服务器上的图像很少,其名称存储在php mysql表中.该表包含两个字段:id和images.我准备了一个 PHP来获取json编码的图像格式如下所述: jsonFetch.php <?php$dbhost = "localhost";$dbname = "userauth";$dbuser = "root";//$DB_Pass = "root";$dbtable = "image 我的服务器上的图像很少,其名称存储在PHP mysql表中.该表包含两个字段:ID和images.我准备了一个 PHP来获取Json编码的图像格式如下所述:

JsonFetch.PHP

<?PHP$dbhost = "localhost";$dbname = "userauth";$dbuser = "root";//$DB_Pass = "root";$dbtable = "images";@MysqL_connect($dbhost,$dbuser);$db = MysqL_select_db($dbname);$sql = "SELECT * FROM $dbtable";$query = MysqL_query($sql);while($row = MysqL_fetch_array($query))    {         $rows[] = array(        //"ID" => $row[0],"image" => $row[1]        //"description" => $row['description']);        );    }$Json = Json_encode($rows);$callback = $_GET['images'];echo $callback.$Json ;   //print_r($Json);  ?>

现在,当我点击网址时,我得到了以下回复:

[{ “图像”:” ./ 95462 “},{” 图像 “:” ./ 8838upload_image.jpg “} {” 图像 “:” ./ 43185upload_image.jpg “},{” 图像 “:”/ 17426upload_image.jpg “}]

我正在获得如上所述的Json数组.

下一步是在UItableVIEw中以多线程方式显示上面的数组.
当我对它们进行硬编码时,我从网址获取图像,但是当涉及到Json解析时,我就是一个菜鸟.我已经尝试了解析Json的所有方式,以便您参考,我发布.m文件. :

#import "Json.h"@interface profilePhotos(Private)- (voID) initialize;- (voID) loadImage:(ID)arg;- (voID) updatetableVIEw:(ID)arg;- (voID) addImagesToQueue:(NSArray *)images;- (voID) addImagesToQueue:(NSArray *)arrayImages;- (voID) addImagesToQueue:(NSArray *)arrayDataFromServer;- (voID) showcommentVIEw;- (voID) hIDecommentVIEw;@end@implementation profilePhotos@synthesize photostable;@synthesize addPhotos;@synthesize deletePhotos;@synthesize back;@synthesize imageQueue,loadedImages,imageLoaderOpQueue,commentVIEw;//@synthesize photosVIEw;-(voID)initializeWith:(int)buttonTag{tag = buttonTag;NSLog(@"tag = %d",tag); }- (ID) initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil {  if (!(self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil])) {    return self;  } [self initialize]; return self;  } - (voID) awakeFromNib {   NSLog(@"AsyncImageLoadingVIEwController::awakeFromNib called");   [super awakeFromNib];    [self initialize];   } - (voID) vIEwDIDLoad {NSLog(@"AsyncImageLoadingVIEwController::vIEwDIDLoad called");[super vIEwDIDLoad]; } - (voID) vIEwDIDAppear:(BOol)animated{ NSLog(@"AsyncImageLoadingVIEwController::vIEwDIDAppear called"); [super vIEwDIDAppear:animated]; NSArray *images = [NSArray arrayWithObjects:                   @"http://dl.dropBox.com/u/9234555/avatars/ava01.gif",@"http://dl.dropBox.com/u/9234555/avatars/ava02.gif",@"http://dl.dropBox.com/u/9234555/avatars/ava03.gif",@"http://dl.dropBox.com/u/9234555/avatars/ava04.gif",@"http://dl.dropBox.com/u/9234555/avatars/ava05.gif",nil];[self addImagesToQueue:images];  NSLog(@"addImagesToQueue: %@",self); } #pragma mark - #pragma mark Private Methods  /*!  @method       @abstract   initializes class variables  */ - (voID) initialize    {      NSLog(@"AsyncImageLoadingVIEwController::initialize called");      NSMutableArray *a = [[NSMutableArray alloc] init];      self.imageQueue = a;      //[a release];      a = [[NSMutableArray alloc] init];      self.loadedImages = a;      //[a release];      NSOperationQueue *queue = [[NSOperationQueue alloc] init];      self.imageLoaderOpQueue = queue;      //[queue release];       }        /*!       @method            @abstract   updates tablevIEw for the newly downloaded image and scrolls the          tablevIEw to bottom       */     - (voID) updatetableVIEw:(ID)arg        {         NSLog(@"AsyncImageLoadingVIEwController::updatetableVIEw called");         if ((arg == nil) || ([arg isKindOfClass:[UIImage class]] == NO)) {            return;    }    // store the newly downloaded image    [self.loadedImages addobject:arg];    //[arg release];    // refresh tablevIEw    [self.photostable reloadData];    // scroll to the last cell of the tablevIEw    NSIndexPath *lastRow = [NSIndexPath indexPathForRow:([self.loadedImages count] - 1) inSection:0];    [self.photostable scrollToRowAtIndexPath:lastRow                            atScrollposition:UItableVIEwScrollpositionBottom                                    animated:YES];} /*! @method      @abstract   downloads images,this is the method that dispatches tasks in the operation q ueue */- (voID) loadImage:(ID)arg {   NSLog(@"AsyncImageLoadingVIEwController::loadImage called");   if ((arg == nil) || ([arg isKindOfClass:[Nsstring class]] == NO)) {    return;    }    // create a local autorelease pool since this code runs not on main thread    //NSautoreleasePool *pool = [[NSautoreleasePool alloc] init];    // fetch the image    NSLog(@"AsyncImageLoadingVIEwController::loadImage - will download image: %@",arg);    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:arg]];    UIImage *image = [UIImage imageWithData:data];    NSLog(@"image: %@",image);    // update tablevIEw with the downloaded image on main thread    [self performSelectorOnMainThread:@selector(updatetableVIEw:) withObject:image    waitUntilDone:NO];    //[pool release];     }  /*!  @method       @abstract   adds images to the queue and starts the operation queue to download them   */ - (voID) addImagesToQueue:(NSArray *)images   {    NSLog(@"AsyncImageLoadingVIEwController::addImagesToQueue called");    [self.imageQueue addobjectsFromArray:images];    NSLog(@"addImagesToQueue Array: %@",self);    // suspend the operation queue    [self.imageLoaderOpQueue setSuspended:YES];    // add tasks to the operation queue     for (Nsstring *imageUrl in self.imageQueue) {     NSInvocationoperation *op = [[NSInvocationoperation alloc] initWithTarget:self                                              selector:@selector(loadImage:) object:imageUrl];      [self.imageLoaderOpQueue addOperation:op];      // [op release];       }// clear items in the queue and resume the operation queue to start downloading images[self.imageQueue removeAllObjects];[self.imageLoaderOpQueue setSuspended:NO];    } #pragma mark - #pragma mark UItableVIEwDataSource Methods  - (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw  numberOfRowsInSection:(NSInteger)section    {return [self.loadedImages count];       }  - (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw     cellForRowAtIndexPath:(NSIndexPath *)indexPath      {    static Nsstring *CellIDentifIEr = @"CellIDentifIEr";cell = [tableVIEw dequeueReusableCellWithIDentifIEr:CellIDentifIEr];if (cell == nil){           //cell = [[[UItableVIEwCell alloc] initWithStyle:UItableVIEwStyleGrouped      reuseIDentifIEr:CellIDentifIEr] autorelease];    cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwStyleGrouped   reuseIDentifIEr:[Nsstring stringWithFormat:@"cellID%d",indexPath.row]];    cell.accessoryType =UItableVIEwCellAccessoryNone;    //cell.accessoryType =UItableVIEwCellAccessorydisclosureIndicator;  }for(UIVIEw *subvIEws in cell.subvIEws)    [subvIEws removeFromSupervIEw];     UIImageVIEw *photo;     photo=[[UIImageVIEw alloc] init];     [photo setimage:[self.loadedImages objectAtIndex:indexPath.row]];     [photo setFrame:CGRectMake(0,5,150,120)];     [cell addSubvIEw:photo];       return cell;       }    -(voID)aMethod:(UIbutton *)sender{//[sender tag];NSIndexPath *indexPath = [photostable indexPathForCell: (UItableVIEwCell*)[[sender supervIEw]supervIEw]];NSLog(@"[sender tag] is %d",[sender tag]);if([sender tag]==indexPath.row){    textFIEld = (UITextFIEld*)[cell vIEwWithTag:[sender tag]];    textFIEld.hIDden=NO;    }  //}    }#pragma mark -#pragma mark UItableVIEwDelegate Methods-(voID)tableVIEw:(UItableVIEw *)tableVIEwdIDSelectRowAtIndexPath:(NSIndexPath *)indexPath{[tableVIEw deselectRowAtIndexPath:indexPath animated:YES];} - (voID)dIDReceiveMemoryWarning   {   // Releases the vIEw if it doesn't have a supervIEw.   [super dIDReceiveMemoryWarning];    // Release any cached data,images,etc that aren't in use.     }   - (voID)vIEwDIDUnload{     // [self setPhotosVIEw:nil];  [self setPhotostable:nil];  [self setAddPhotos:nil];  [self setDeletePhotos:nil];  [self setBack:nil];  [super vIEwDIDUnload];  // Release any retained subvIEws of the main vIEw.  // e.g. self.myOutlet = nil;      }   - (BOol)shouldautorotatetoInterfaceOrIEntation:(  UIInterfaceOrIEntation)interfaceOrIEntation      {       // Return YES for supported orIEntations      return (interfaceOrIEntation == UIInterfaceOrIEntationPortrait);       }

我相信在vIEwDIDAppear方法中需要做一些事情,但我不明白它是什么.

请帮助我.我已经尝试了所有可能的Json方法.可能是我在那里犯了一些错误,但我一直很沮丧.请帮帮我.

解决方法 Dude ..使用以下参考,您将获得教程作为工作演示..

images in UITableView using multithreading

See this reference if you are new in iOS. It’s simple

希望对你有帮助…

总结

以上是内存溢出为你收集整理的iphone – 如何使用多线程在UITableView中显示json图像数组?全部内容,希望文章能够帮你解决iphone – 如何使用多线程在UITableView中显示json图像数组?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1021436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存