0

我有一个 UITable,我喜欢在单元格的详细视图中添加图像。我可以处理从相机胶卷或相机中选择任何图像:

cell.imageView.image = someImage;

但是我如何定义一个特定的图像 - 在上面的例子中:“someImage”,以便下次运行应用程序时,为每个项目显示正确的图像。

更新。这是我用来捕捉/选择图像的代码..

- (IBAction)btnTakePicture_Clicked:(id)sender
{
    NSLog(@"%s", __FUNCTION__);
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image from..." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Image Gallary", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.alpha=0.90;
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%s", __FUNCTION__);
    switch (actionSheet.tag)
    {
        case 1:
            switch (buttonIndex)
        {
            case 0:
            {
#if TARGET_IPHONE_SIMULATOR

                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Ooops" message:@"Camera not available." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];

#elif TARGET_OS_IPHONE

                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                picker.delegate = self;
                //picker.allowsEditing = YES;
                [self presentViewController:picker animated:YES completion:nil];


#endif
            }
                break;
            case 1:
            {
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                picker.delegate = self;
                [self presentViewController:picker animated:YES completion:nil];
            }
                break;
        }
            break;

        default:
            break;
    }
}


-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    NSLog(@"%s", __FUNCTION__);
    dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
    imgPicture.image = [[UIImage alloc] initWithData:dataImage];

    [picker dismissViewControllerAnimated:YES completion:nil];
}

更新 2. 在下面的人的帮助下,我认为这个解决方案对我有用:

- (IBAction)photoLibraryAction:(id)sender
{   
    int c = self.capturedImages.count;
    for (int i=0; i < c; i++ ){
        if (self.imageView.tag == cellTag) {
            NSLog(@"found it");
    } else {
        NSLog(@"can't find it");
    }
    }
}


 if ([self.capturedImages count] == 1)
        {
            // we took a single shot
            [self.imageView setImage:[self.capturedImages objectAtIndex:0]];
          [self.imageView setTag:myTag];
        }
4

1 回答 1

1

我会这样做。

@interface CustomClassCell : UITableViewCell
  @property (unsafe_unretained, nonatomic) IBOutlet UIImageView *HomePicture;
@end

@interface CustomClass : NSObject
   @property (nonatomic, copy) NSString *PicPath;
@end


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath      
{
   CustomClassCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomClassCellIdentifer"];
   id object = [self.tableData  objectAtIndex:indexPath.row];
   CustomClass *myObject = (CustomClass*)object;
   cell.HomePicture.image = [UIImage imageNamed:myObject.PicPath];
   return cell;
}
于 2013-01-28T15:19:53.630 回答