我有几个按钮。当按下任一按钮时,它会弹出并 alertView 要求用户拍照或从相机胶卷中选择。现在我遇到的问题是我有 12 个按钮和 12 个 UIImageViews。所有按钮都有自己的动作,可以弹出警报并允许用户选择任一选项。现在在 didFinishPickingMediaWithInfo 方法中,我将图像从相机或相机胶卷传递到第一个 imageView。这一切都很好,但是如果我想选择按钮 2,它会触发另一个带有另一个标签的警报,我想设置 imageView 2 等等(不替换 imageView1)。我需要一种方法在 didFinishPickingMediaWithInfo 中根据弹出警报的按钮选择来区分要设置的 imageView。
这是按钮的操作。
-(IBAction) addPhoto1:(id) sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Image Source" message:@"Take a photo or select a previously taken photo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Take Photo", @"Select Photo", nil];
[alert show];
alert.tag = 101;
[alert release];
}
并警告clickedButtonAtIndex
:
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alert.tag == 101) {
if (buttonIndex == 1) {
//Take photo
[self performSelector:@selector(takePicture:) withObject:nil afterDelay:0.0];
}
else if (buttonIndex == 2){
//Camera roll
[self performSelector:@selector(pictureAlbum:) withObject:nil afterDelay:0.0];
}
else if (buttonIndex == 0) {
NSLog(@"Cancel");
}
}
}
这是didFinishPickingMediaWithInfo
:
-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] metadata:dict completionBlock:nil];
if (addFirstImage.tag == 1001) {
firstImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
firstImage.layer.cornerRadius = 5;
firstImage.layer.masksToBounds = YES;
}
if (addSecondImage.tag == 1002) {
secondImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
secondImage.layer.cornerRadius = 5;
secondImage.layer.masksToBounds = YES;
}
}
}
现在显然这是不对的,根据最初按下的警报按钮设置正确的 imageViews 图像的最佳方法是什么?(addFirstImage 和 addSecondImage 都是通过 IB 链接的按钮)
非常感谢