我有一个设置视图来更改个人资料图片以及封面图片, 我把它设置成这样:
- (IBAction)coverPressed:(id)sender
{
buttonPressed = 1;
if (self.image == nil) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self performSelector:@selector(takePhoto) withObject:nil];
} else {
[self performSelector:@selector(chooseFromLibrary) withObject:nil];
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
}
- (void) avatarPressed:(id)sender
{
buttonPressed = 2;
if (self.image == nil) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self performSelector:@selector(takePhoto) withObject:nil];
} else {
[self performSelector:@selector(chooseFromLibrary) withObject:nil];
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
}
-(void)takePhoto
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate,
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)chooseFromLibrary
{
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc]init];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// image picker needs a delegate so we can respond to its messages
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (buttonPressed == 2) {
[self dismissViewControllerAnimated:YES completion:nil];
image = [info objectForKey:UIImagePickerControllerOriginalImage];
avatar = [image thumbnailImage:200 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationMedium];
avatarSmall = [image thumbnailImage:40 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationMedium];
}
if (buttonPressed == 1) {
[self dismissViewControllerAnimated:YES completion:nil];
image = [info objectForKey:UIImagePickerControllerOriginalImage];
cover = [image thumbnailImage:320 transparentBorder:0 cornerRadius:0 interpolationQuality:kCGInterpolationMedium];
}
}
但如果按下"更改个人资料图片" UIButton并选择一张图片,我再也无法按下"更改封面图片"或者反之,我在我的代码中做了什么以使它只出现一次有什么不对吗?
答案 0 :(得分:0)
在这些方法中......
- (void) avatarPressed:(id)sender
{
buttonPressed = 2;
if (self.image == nil) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self performSelector:@selector(takePhoto) withObject:nil];
} else {
[self performSelector:@selector(chooseFromLibrary) withObject:nil];
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
}
您正在执行几个选择器,它们自己呈现一个图像选择器,然后直接在那些if语句之后呈现另一个图像选择器。
认为你的方法应该是这样的
- (void) avatarPressed:(id)sender
{
buttonPressed = 2;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self performSelector:@selector(takePhoto) withObject:nil];
} else {
[self performSelector:@selector(chooseFromLibrary) withObject:nil];
}
}