从相机胶卷拍摄的图像需要很长时间才能在uitableview中显示

时间:2012-07-16 12:11:49

标签: iphone objective-c ios uitableview uiimagepickercontroller

我正在使用此类从UIImagePickerController/Camera获取图像,将它们存储在文档目录中并将其显示给用户。基本上这些代码是有效的,但是在用户选择图像(至少10秒以上)后,在UITableView中实际显示数据需要很长时间。

这是我的班级(不是完整的班级,只是问题代码):

#import "ReceiptImageViewController.h"
#import "ReceiptImageCell.h"

@interface ReceiptImageViewController ()

@end

@implementation ReceiptImageViewController

@synthesize fields;
@synthesize receiptNumber;
@synthesize delegate;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    images = [[NSMutableArray alloc] init];

    //Set UI Colors
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationController.toolbar.tintColor = [UIColor blackColor];
}

- (void) viewDidAppear:(BOOL)animated{
    NSLog(@"ViewDidAppear");
    [self.tableView reloadData];
    NSLog(@"Table Data Reloaded");
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    [images removeAllObjects];
    for (NSString *path in fields)
    {
        if (![path isEqualToString:@""])
        {
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            [images addObject:image];
            NSLog(@"Adding Image");
        }
        NSLog(@"Loop");
    }
    return [images count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ReceiptImageCell";
    ReceiptImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    //Set info label text
    cell.infoLabel.text = [NSString stringWithFormat:@"Receipt Image: %d", indexPath.row];
    cell.receiptImage.image = [images objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark - UIActionSheet Delegate

//Displays action sheet when the user presses
//'+' button next to image view
-(IBAction)showActionSheet:(id)sender{

    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Receipt Image" 
                                                            delegate:self 
                                                   cancelButtonTitle:@"Cancel"
                                              destructiveButtonTitle:NULL 
                                                   otherButtonTitles:@"Take Picture", @"Choose From Library", nil];
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    popupQuery.tag = 0;
    [popupQuery showInView:self.view];

    //    //FOR TESTING ON SIMULATOR 
//    UIImage* image = [UIImage imageNamed:@"Receipt1.jpg"];
//    int i = [self.tableView numberOfRowsInSection:0] + 1;
//    NSString* imageName = [NSString stringWithFormat:@"Receipt%@Image%d", receiptNumber, i];
//    [self saveImage:image :imageName];
}

//Saves receipt image to documents directory
- (void)saveImage:(UIImage*)image:(NSString*)imageName 
{ 
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.

    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it

    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 

    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

    [fields addObject:fullPath];

    [self.tableView reloadData];

    NSLog(@"image saved");
}

#pragma mark - UIImagePickerControllerDelegate

//Receive the image the user picks from the image picker controller
-(void)imagePickerController:(UIImagePickerController*)picker
didFinishPickingMediaWithInfo:(NSDictionary*)info {
    // Do stuff to image.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        UIImage* image = [info objectForKey: UIImagePickerControllerOriginalImage];
        int i = [self.tableView numberOfRowsInSection:0] + 1;
        NSString* imageName = [NSString stringWithFormat:@"Receipt%@Image%d", receiptNumber, i];
        [self saveImage:image :imageName];
    });

    [self dismissModalViewControllerAnimated:YES];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (actionSheet.tag == 0){
        if (buttonIndex == 0) {
            NSLog(@"Take Picture Button Clicked");
            // Create image picker controller
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

            // Set source to the camera
            imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

            // Delegate is self
            imagePicker.delegate = self;

            // Show image picker
            [self presentModalViewController:imagePicker animated:YES];
        } 
        else if (buttonIndex == 1) {
            NSLog(@"Choose From Library Button Clicked");
            // Create image picker controller
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

            // Set source to the camera
            imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

            // Delegate is self
            imagePicker.delegate = self;

            // Show image picker
            [self presentModalViewController:imagePicker animated:YES];
        } 
        else if (buttonIndex == 2) {
            NSLog(@"Cancel Button Clicked");
        } 
    }
}

任何人都能解释为什么会这样吗?我在其他应用程序中遇到了这个问题,并且从未设法找到解决方案。即使涉及使用活动指标或类似问题?

非常感谢,

杰克

0 个答案:

没有答案