从相机胶卷拍摄的图像,需要长时间的UITableView显示(Images taken from c

2019-10-17 00:20发布

我使用这个类来拍摄图像从UIImagePickerController/Camera ,它们存储在文件目录中,并显示给用户。 本质上,这代码有效,但它需要很长的时间来实际在UITableView中显示的数据的用户选择的图像(10+秒至少)之后。

这里是我的类(不是满级,只是问题的代码):

#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");
        } 
    }
}

谁能解释为什么出现这种情况? 我已经运行其他应用程序的这个问题,从来没有设法找到了一个solution..Even涉及使用活动指标或类似的?

非常感谢,

插口

文章来源: Images taken from camera roll take long time to display in uitableview