Подтвердить что ты не робот

Как загружать фотографии из фотогалереи и хранить ее в проекте приложения?

Я работаю над приложением, которое почти совпадает с примерами кодов, которые я нашел здесь. Но метод, который я хочу сделать, отличается от кода примера.
Ссылка: PhotoLocations

На первом экране будут кнопки ImageView и 2 (выберите "Фото", "Подтвердить" ). Когда кнопка "Выбрать фотографию" будет нажата, она перейдет на другой экран, который будет извлекать фотографии из моей фотогалереи iPad. Когда выбрана фотография, она отключит текущий экран и вернется к первому экрану, отображающему выбранную фотографию в ImageView.

Когда нажата кнопка "Подтвердить", фотография будет сохранена в моем проекте приложения (например,/resources/images/photo.jpg).

Могу ли я узнать, как я могу это сделать?

4b9b3361

Ответ 1

Это приведет вас к галерее изображений, и вы можете выбрать изображение.

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];

это поможет вам выбрать изображение

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
    // Dismiss the image selection, hide the picker and

    //show the image view with the picked image

    [picker dismissViewControllerAnimated:YES completion:nil];
    //UIImage *newImage = image;
}

И затем вы можете сохранить это изображение в каталоге документов...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];

Чтобы щелкнуть изображение самостоятельно, используйте

- (IBAction) takePhoto:(id) sender
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:imagePickerController animated:YES];
}

Ответ 2

UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init];
cardPicker.allowsEditing=YES;
cardPicker.delegate=self;
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:appDelegate.cardPicker animated:YES];
[cardPicker release];

И этот метод делегата вернет изображение u из альбома

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    noImage.image=img;
}

Ответ 3

- (IBAction)UploadPhoto:(id)sender {
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please Select Your Option"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery",nil];
    
    [actionSheet showInView:self.view];
    
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    
    NSIndexPath *clickedButtonPath = [jobstable indexPathForCell:clickedCell];
    
    isSectionIndex = clickedButtonPath.section;
    isRowIndex     = clickedButtonPath.row;
    
}


-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
    
    NSLog(@"From didDismissWithButtonIndex - Selected Option: %@", [actionSheet buttonTitleAtIndex:buttonIndex]);
    
    NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex];
    
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) {
        
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            return;
        }
        else{
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:picker animated:YES completion:NULL];
            
        }
    }
    
    else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Gallery"]){
        
        
        UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
        pickerView.allowsEditing = YES;
        pickerView.delegate = self;
        [pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        [self presentViewController:pickerView animated:YES completion:nil];
        
    }
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex];
        
        UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];
        
        UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];
        
        tableIMAGE.image=orginalImage;
        
  answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@,",answersARRAY[indexPath.row],imageStris];

    
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
        
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        
        [picker dismissViewControllerAnimated:YES completion:NULL];
    
    }