从iphone真实设备拍摄的照片在缩略图中显示的图像在iphone中花费的时间太长

时间:2013-04-12 06:27:15

标签: iphone ios

我是手机编程的新手。我已将图像存储在阵列中,然后使用下面的代码我在缩略图中显示图像,工作速度非常快。同样认为我将图像存储在数据库中并检索图像并存储在数组中然后使用下面的代码我在缩略图中显示图像,显示缩略图图片的时间太长。

  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;

使用数据库下面的代码我在数组中存储图像然后我在缩略图中显示图像。在缩略图中显示图像需要太长时间。

    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)];
        }

我认为问题不在于数据库。主要的想法是,如果我从手机真实设备拍摄图像需要太长时间才能在缩略图中显示。如果我拍摄一些图像并存储在数组中,图像以缩略图显示其工作非常快.. 任何人都可以告诉我如何解决这个问题。 谢谢 阿斯拉姆

1 个答案:

答案 0 :(得分: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();