从相机ios中选择图像时,在tableview中加载不同的图像

时间:2015-07-22 11:14:37

标签: ios objective-c uitableview

我有一个相机按钮,我从手机库中选择图像,在表格视图中,我有另一个视图,我想在其中显示所选图像。我能够在tableview单元格中获得相同的图像。但我想要不同的图像。这对我有什么帮助??????????任何人都可以找我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [createDelvTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSArray *subviewArray = cell.contentView.subviews;
    for (UIView *view in subviewArray)
    {
        [view removeFromSuperview];
    }


    UILabel *picLabels = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 60)];
    picLabels.text = listItems[indexPath.row];
    picLabels.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
     picLabels.font =[UIFont systemFontOfSize:13];
    [cell.contentView addSubview:picLabels];


    imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
    imageParcel.userInteractionEnabled=YES;
    imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
    imageParcel.image = [imageArray objectAtIndex:0];
    imageParcel.layer.cornerRadius = 30;
    imageParcel.layer.masksToBounds = YES;
    //cell.imageView.image = [imageArray objectAtIndex:0];
    [cell.contentView addSubview:imageParcel];

    parcelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    parcelButton.frame = CGRectMake(180, 0, 50, 50);
    [parcelButton setBackgroundImage:[UIImage imageNamed:@"Camera.png"]forState:UIControlStateNormal];
    parcelButton.tag = indexPath.row;
    [parcelButton addTarget:self action:@selector(addPictureAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.accessoryView = parcelButton;



    return cell;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


    chosenImage = info[UIImagePickerControllerEditedImage];

    imageParcel.image = chosenImage;


    [createDelvTableView reloadData]; //call reloadData method to load data with image
    UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);

    imageArray = @[chosenImage];

    [picker dismissViewControllerAnimated:YES completion:NULL];

}
- (void)addPictureAction:(UIImage*)image
{
    NSLog(@"Add Pictures");

    // if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {



    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.delegate = self;
    photoPicker.allowsEditing = YES;
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:photoPicker animated:YES completion:NULL];


    // }else
{



    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                          message:@"Device has no camera"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles: nil];

    [myAlertView show];

}

1 个答案:

答案 0 :(得分:1)

ViewDidLoad代码

imageArray =[[NSMutableArray alloc]init];
    for (int i=0; i<listItems.count; i++) {
        [imageArray addObject:[UIImage new]];
    }

首先从相机中选择图像,您可以像这样在NSMutableArray imageArray中添加图像

[imageArray replaceObjectAtIndex:index withObject:chosenImage]
[createDelvTableView reloadData];// reload tableview Data 

并且tableView Delegate方法代码以这种方式设置图像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      //your code here con..
      imageParcel.image = [imageArray objectAtIndex:indexPath.row];
}

按钮点击代码

- (IBAction)addPictureAction:(UIButton*)image
{
     index=image.tag; // declare long index variable .h file
}