我在同一个视图中有两个UIImageView。对于每个imageView,我应该分配从相机或照片库中拍摄的照片。 当我开始这样做时,我为两个imageView获得了相同的照片。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}
-(IBAction)addPhoto:(id)sender{
UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"Choose Photo" otherButtonTitles:@"Take Photo ", nil];
[actionSheet showInView:self.view];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
shouldUpdateFirstImage =YES;
// chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
if (shouldUpdateFirstImage) {
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = NO;
}
else {
pictureView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = YES;
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Put Picture"])
{
alrtView.hidden = YES;
pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:pictureView];
[self addPhoto:self];
}
和viewController.h
@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,UITextViewDelegate>
{
BOOL shouldUpdateFirstImage;
}
第一个ImageView是背景。通过AlertView,我选择将Picture添加到第一个ImageView。之后我回想起相同的动作表来选择拍摄的照片。
答案 0 :(得分:0)
TL; DR您需要跟踪班级的状态,并让您的委托方法相应地响应该州。
听起来您需要班级跟踪已显示的图像数量,并根据该信息加载新选择。我们称之为“状态”,您必须为imagePickerController委托方法实现逻辑以适当地响应该状态。
一种可能的方法是将一个BOOL作为您的类的范围(ivar或属性可以做,但ivar可能更合适),然后使用它的值来跟踪应该更新哪个imageView。
假设您已经声明了一个名为shouldUpdateFirstImage的BOOL,您可以这样做:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
if (shouldUpdateFirstImage) {
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = NO;
}
else {
imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
shouldUpdateFirstImage = YES;
}
}
这将替换每个连续选择更新的imageView。
答案 1 :(得分:0)
试试这个
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
if(imageView.image)
imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
else
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}