Domanda

I have a Settings view to change a profile picture as well as a cover picture, I got it set up like this:

- (IBAction)coverPressed:(id)sender
{
    buttonPressed = 1;
    if (self.image == nil) {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = NO;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self performSelector:@selector(takePhoto) withObject:nil];
        } else {
            [self performSelector:@selector(chooseFromLibrary) withObject:nil];
        }

        self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
        [self presentViewController:self.imagePicker animated:NO completion:nil];
    }
}

- (void) avatarPressed:(id)sender
{
    buttonPressed = 2;
    if (self.image == nil) {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = NO;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self performSelector:@selector(takePhoto) withObject:nil];
        } else {
            [self performSelector:@selector(chooseFromLibrary) withObject:nil];
        }

        self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
        [self presentViewController:self.imagePicker animated:NO completion:nil];
    }
}

-(void)takePhoto
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
    }

    // image picker needs a delegate,
    [imagePickerController setDelegate:self];

    // Place image picker on the screen
    [self presentViewController:imagePickerController animated:YES completion:nil];
}



-(void)chooseFromLibrary
{
    UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

    // image picker needs a delegate so we can respond to its messages
    [imagePickerController setDelegate:self];

    // Place image picker on the screen
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if (buttonPressed == 2) {
        [self dismissViewControllerAnimated:YES completion:nil];
        image = [info objectForKey:UIImagePickerControllerOriginalImage];

        avatar = [image thumbnailImage:200 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationMedium];
        avatarSmall = [image thumbnailImage:40 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationMedium];
    }
    if (buttonPressed == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
        image = [info objectForKey:UIImagePickerControllerOriginalImage];

        cover = [image thumbnailImage:320 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationMedium];
    }

}

But if I press the "change profile picture" UIButton and select an image, I can no longer press the "change cover picture" or viceversa, Is there anything wrong I'm doing in my code to make it appear only once?

È stato utile?

Soluzione

In these methods....

- (void) avatarPressed:(id)sender
{
    buttonPressed = 2;
    if (self.image == nil) {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.allowsEditing = NO;

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
             [self performSelector:@selector(takePhoto) withObject:nil];
        } else {
             [self performSelector:@selector(chooseFromLibrary) withObject:nil];
        }

        self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
        [self presentViewController:self.imagePicker animated:NO completion:nil];
     }
 }

You are performing a couple of selectors, which themselves present an image picker, and then straight after those if statements you are presenting another image picker.

Think your methods should look like this

- (void) avatarPressed:(id)sender
{
    buttonPressed = 2;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [self performSelector:@selector(takePhoto) withObject:nil];
    } else {
        [self performSelector:@selector(chooseFromLibrary) withObject:nil];
    }

 }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top