2013-04-12 22 views
1

我是新手機編程。我有商店圖像數組然後使用下面的代碼我顯示圖​​像縮略圖其工作速度非常快。如果同樣認爲我有數據庫中的存儲圖像和檢索圖像並存儲在數組中,然後使用下面的代碼我顯示圖​​像的縮略圖,它需要太長的時間來顯示縮略圖圖像。拍攝形式iPhone真正的設備,圖像顯示縮略圖在iphone中需要太長時間

arrayz = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"image3.jpg"], 
      [UIImage imageNamed:@"image4.jpg"], 
      [UIImage imageNamed:@"image5.jpg"], 
      [UIImage imageNamed:@"image3.jpg"], 

      [UIImage imageNamed:@"image5.jpg"], 
      [UIImage imageNamed:@"image4.jpg"], 
      [UIImage imageNamed:@"image2.jpg"], 
      [UIImage imageNamed:@"image3.jpg"], 
      [UIImage imageNamed:@"image2.jpg"], 
      [UIImage imageNamed:@"image5.jpg"], 

      nil]; 

    NSLog(@"%i" , [arrayz count]); 
    NSLog(@"%@",arrayz); 

    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)]; 
    myScrollView.delegate = self; 
    myScrollView.contentSize = CGSizeMake(320.0, 840.0); 
    myScrollView.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview:myScrollView]; 

    float horizontal = 8.0; 
    float vertical = 8.0; 

    for(int i=0; i<[arrayz count]; i++) 
    { 
     if((i%4) == 0 && i!=0) 
     { 
      horizontal = 8.0; 
      vertical = vertical + 70.0 + 8.0; 
     } 
     buttonImage1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [buttonImage1 setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)]; 
     [buttonImage1 setTag:i]; 

     [buttonImage1 setImage:[arrayz objectAtIndex:i] forState:UIControlStateNormal]; 
     [buttonImage1 addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [buttonImage1 setImage:[UIImage imageNamed:@"Overlay.png"] forState:UIControlStateSelected]; 
     buttonImage = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)]; 
     [buttonImage setTag:i]; 

     [buttonImage setImage:[arrayz objectAtIndex:i] forState:UIControlStateNormal]; 
     [buttonImage addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [buttonImage setImage:[UIImage imageNamed:@"Overlay.png"] forState:UIControlStateSelected]; 

     // UIImageView *imageee=[[UIImageView alloc]initWithFrame:CGRectMake(10,10, 80,80)]; 
     // [buttonImage setImage:[UIImage imageNamed:@"check.jpg"]]; 

     // [buttonImage setImage:imageee forState:UIControlStateSelected]; 
     // [buttonImage setImage:button.currentImage forState:UIControlStateNormal]; 



     [myScrollView addSubview:buttonImage1]; 
     [myScrollView addSubview:buttonImage]; 

     horizontal = horizontal + 70.0 + 8.0; 
      [myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)]; 
     } 



    // Do any additional setup after loading the view, typically from a nib. 
    self.navigationItem.leftBarButtonItem = self.editButtonItem; 
    // UIBarButtonItem *done =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:nil]; 
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(insertNewObject:)]; 
    self.navigationItem.rightBarButtonItem = done; 
下面代碼

使用數據庫我正在將圖像存儲在陣列那麼我顯示在thumbnail.its圖像花費的時間太長在縮略圖來顯示圖像。

NSString *docsDir; 
    NSArray *dirPaths; 

    // Get the documents directory 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = [dirPaths objectAtIndex:0]; 
    array=[[NSMutableArray alloc]init]; 
    array1=[[NSMutableArray alloc]init]; 
    // array2=[[NSMutableArray alloc]init]; 


    // Build the path to the database file 
    databasePath = [docsDir stringByAppendingPathComponent: @"Taukydataaa.db"]; 
    // NSLog(@"%@",databasePath); 
    NSFileManager *fn=[NSFileManager defaultManager]; 
    NSError *error; 
    BOOL success=[fn fileExistsAtPath:databasePath]; 

    if(!success) { 

     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Taukydataaa.db"]; 
     success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error]; 
    } 


    const char *dbpath = [databasePath UTF8String]; 

    sqlite3_stmt *statement; 

    if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK) 
    { 

     NSString *querySQL = [NSString stringWithFormat: @"select * from path "]; 

     const char *query_stmt = [querySQL UTF8String]; 

     if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) 
     { 
      while(sqlite3_step(statement) == SQLITE_ROW) 
      { 

       NSString* email_idField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,1)]; 
       // NSLog(@"%@",email_idField); 
       NSString* email_idField1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)]; 
       // NSLog(@"%@",email_idField1); 

       //  NSString *email_idField2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,2)]; 
       NSLog(@"ASlma"); 

       NSLog(@"%@",email_idField1); 
       [array addObject:email_idField]; 
       [array1 addObject:email_idField1]; 
       NSLog(@"%@",array); 
       NSLog(@"%@",array1); 
       // [array1 addObject:email_idField1]; 
       // [array2 addObject:email_idField2]; 



      } 
      sqlite3_finalize(statement); 
     } 
     sqlite3_close(contactDB); 

    } 
    blaukypath =[[NSMutableArray alloc]init]; 
    for (NSString* path in array) 
    { 
     [blaukypath addObject:[UIImage imageWithContentsOfFile:path]]; 
     NSLog(@"%@",path); 
    } 
    NSLog(@"%@",blaukypath1); 


    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)]; 
    myScrollView.delegate = self; 
    myScrollView.contentSize = CGSizeMake(320.0, 840.0); 
    myScrollView.backgroundColor = [UIColor whiteColor]; 
    [self.view addSubview:myScrollView]; 

    float horizontal = 8.0; 
    float vertical = 8.0; 

    for(int i=0; i<[blaukypath count]; i++) 
    { 
     if((i%4) == 0 && i!=0) 
     { 
      horizontal = 8.0; 
      vertical = vertical + 70.0 + 8.0; 
     } 
     buttonImage1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [buttonImage1 setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)]; 
     [buttonImage1 setTag:i]; 

     [buttonImage1 setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal]; 
     [buttonImage1 addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [buttonImage1 setImage:[UIImage imageNamed:@"Overlay.png"] forState:UIControlStateSelected]; 
     buttonImage = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)]; 
     [buttonImage setTag:i]; 

     [buttonImage setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal]; 
     [buttonImage addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [buttonImage setImage:[UIImage imageNamed:@"Overlay.png"] forState:UIControlStateSelected]; 

     // UIImageView *imageee=[[UIImageView alloc]initWithFrame:CGRectMake(10,10, 80,80)]; 
     // [buttonImage setImage:[UIImage imageNamed:@"check.jpg"]]; 

     // [buttonImage setImage:imageee forState:UIControlStateSelected]; 
     // [buttonImage setImage:button.currentImage forState:UIControlStateNormal]; 



     [myScrollView addSubview:buttonImage1]; 
     [myScrollView addSubview:buttonImage]; 

     horizontal = horizontal + 70.0 + 8.0; 
      [myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)]; 
     } 

我認爲問題不在於database.What主認爲,如果我採取圖片來自我的手機,圖像的時間過長在thumbnail.If顯示我採取一些陣列圖像和商店,圖像顯示真實設備在縮略圖其工作非常快.. 任何機構可以告訴我如何解決這個問題。 謝謝 阿斯拉姆

回答

1

真正的圖像將有更多的大小,所以更好地調整它們之前使用它們。試試這個

UIImage *originalImage = your orginal Image 
CGSize destinationSize = CGSizeMake(100, 120);// your destination thumbnail size 

UIGraphicsBeginImageContext(destinationSize); 
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)]; 
newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

非常感謝你給予重播它的工作f9。 – Aslam

相關問題