ios – UIImagePickerController Taking Seconds to Appear


On my app, I have two UIImageViews with interaction enabled. They each initially load with a placeholder image. Tapping them is supposed to open the image picker controller. Selecting an image lets them crop it and choose, at which point the UIImageView image changes to that. This all works, but it is around 3-5 seconds AFTER tapping the UIImageView before the controller appears. I tested this with just a simple UIButton as well, but it had the same results. On a separate view controller I tried it, and it was instantaneous, even with the exact same code. What am I missing?

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImageForFirstImageView:)];
    tapGestureRecognizer1.numberOfTapsRequired = 1;

       [self.firstImageView addGestureRecognizer:tapGestureRecognizer1];
       self.firstImageView.userInteractionEnabled = YES;
       
       UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImageForSecondImageView:)];
    tapGestureRecognizer2.numberOfTapsRequired = 1;

       [self.secondImageView addGestureRecognizer:tapGestureRecognizer2];
       self.secondImageView.userInteractionEnabled = YES;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.pdfFileArray = [NSMutableArray array];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    self.firstImageView.image = [UIImage imageNamed:@"naughtyplaceholder"];
        self.secondImageView.image = [UIImage imageNamed:@"niceplaceholder"];
    self.firstImageView.tag = 1;
        self.secondImageView.tag = 2;

    // Set up your right bar button item
    UIBarButtonItem *buildButton = [[UIBarButtonItem alloc] initWithTitle:@"Build" style:UIBarButtonItemStylePlain target:self action:@selector(buildPDF)];
    self.navigationItem.rightBarButtonItem = buildButton;
    
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];

    
    
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth) fromDate:currentDate];
    NSInteger currentYear = [components year];
       NSInteger currentMonth = [components month];
       
       if (currentMonth <= 7) {
           currentYear -= 1; // If it's August or later, increment the year
       }
    // Extract the year and month components
    NSInteger year = [components year];

    // Get the entered name from the textField

    // Create the folder path based on the year
    NSString *folderPath = [NSString stringWithFormat:@"%@/NaughtyNice/%@", [self documentsDirectory], [@(currentYear) stringValue]];
    
    NSArray *files = [fileManager contentsOfDirectoryAtPath:folderPath error:nil];
    NSPredicate *pdfFilter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.pdf'"];
    NSArray *pdfFiles = [files filteredArrayUsingPredicate:pdfFilter];
    
    
    
    
    // Assuming pdfFileArray is an NSMutableArray to store PDF file names
    NSLog(@"Naughty Path %@", folderPath);

    self.pdfFileArray = [NSMutableArray arrayWithArray:pdfFiles];

    NSLog(@"PDF File Array Count: %lu", (unsigned long)self.pdfFileArray.count);

    [self.tableView reloadData];

    
    
    
    
}
- (void)selectImageForFirstImageView:(UITapGestureRecognizer *)gesture {
    NSLog(@"Tapped");
    [self presentImagePickerForImageView:self.firstImageView];
}

- (void)selectImageForSecondImageView:(UITapGestureRecognizer *)gesture {
    [self presentImagePickerForImageView:self.secondImageView];
}

- (void)presentImagePickerForImageView:(UIImageView *)imageView {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.allowsEditing = YES;

    // Set a tag or other identifier to distinguish between image views
    imagePicker.view.tag = imageView.tag;

    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    UIImage *selectedImage = info[UIImagePickerControllerEditedImage];

    // Assign the selected image to the appropriate UIImageView
    if (picker.view.tag == self.firstImageView.tag) {
        self.firstImageView.image = selectedImage;
    } else if (picker.view.tag == self.secondImageView.tag) {
        self.secondImageView.image = selectedImage;
    }

    [picker dismissViewControllerAnimated:YES completion:nil];
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img