[英]how I can assign for each UIImageView in the same view a picture taken from camera?
I have two UIImageView in the same view.for each imageView I should assign a picture taken from the camera or photos library. 我在同一个视图中有两个UIImageView。对于每个imageView,我应该分配一个从相机或照片库拍摄的图片。 When I start by doing that, I get the same photo for the two 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];
}
And the viewController.h 还有viewController.h
@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,UITextViewDelegate>
{
BOOL shouldUpdateFirstImage;
}
The first ImageView is the background.Through an AlertView I choose to add Picture to the first ImageView.After that I recall the same actionsheet to choose the taken photo. 第一个ImageView是背景。通过AlertView,我选择将图片添加到第一个ImageView。之后,我调出相同的操作表来选择拍摄的照片。
TL;DR you need to track the state of your class, and have your delegate method respond accordingly to that state. TL; DR您需要跟踪类的状态,并让您的委托方法对状态进行相应的响应。
It sounds like you need your class to keep track of how many images have been displayed, and load new selections based on that information. 听起来您需要您的班级来跟踪已显示的图像数量,并根据该信息加载新的选择。 We call this "state", and you have to implement the logic for your imagePickerController delegate method to respond appropriately to that state.
我们将其称为“状态”,并且您必须为imagePickerController委托方法实现逻辑以适当地响应该状态。
One possible approach would be to have a BOOL scoped to your class (either an ivar or property will do, but ivar is likely more appropriate), then use it's value to keep track of which imageView should be updated. 一种可能的方法是将BOOL范围限定在您的类上(可以使用ivar或属性,但使用ivar可能更合适),然后使用其值来跟踪应该更新哪个imageView。
Assuming you already declared a BOOL named shouldUpdateFirstImage, you could do something like this: 假设您已经声明了一个名为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;
}
}
This would alternate which imageView was updated with each successive selection. 这将交替使用每个连续的选择更新哪个imageView。
Try this 尝试这个
- (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"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.