关于点击按钮打开照相机或者相册的方法

直接贴上代码。
之前做的时候因为按钮的点击事件,在上传图片的时候每次都只能加载在同一个按钮上面,另一个打开了点击上传图片的时候,仍然只能加载在第一个按钮,发现是没有判断到底点击的是哪个按钮。
所以,先定义一个按钮为当前按钮
@property (nonatomic,strong) UIButton * currentBtn;

如果你有三个按钮都要执行同一个操作,也就是打开相机获取图片,我的做法是三个按钮的点击事件都关联为一个,然后给每个按钮一个tag值,我给的分别是1 2 3 。然后判断。

//商品封面添加图片点击事件
- (IBAction)Add_Image:(UIButton *)sender {
    switch (sender. tag) {
        case 1:
        {
            self.currentBtn = self.Add_Image_1;
            [self CreateActionSheet];
        }
            break;
        case 2:
        {
            self.currentBtn = self.Add_Image_2;
            [self CreateActionSheet];
        }
            break;
        case 3:
        {
            self.currentBtn = self.Add_Image_Detail;
            [self CreateActionSheet];
        }
        default:
            break;
    }
}

#pragma mark - 创建actionSheet的方法
- (void)CreateActionSheet{

    UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:nil
                                                              delegate:self
                                                     cancelButtonTitle:@"取消"
                                                destructiveButtonTitle:nil
                                                     otherButtonTitles:@"拍照",@"从相册中选择",@"删除该照片",nil];

    [actionSheet showInView:self.view];
}

上面为判断点击的是哪个按钮,然后把点击的这个按钮设置为当前点击按钮,也就是currentBtn。
下面的方法就是创建actionSheet的方法。

接下来就是调用代理方法了。

遵守协议
UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate,

实现协议方法

#pragma mark - actionSheetDelegate
//点击某个button时调用的方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"buttonIndex = %ld",(long)buttonIndex);

    switch (buttonIndex) {
        case 0:
        {
            NSLog(@"拍照");
            [self openCamera];
        }
            break;
        case 1:
        {
            NSLog(@"从相册中选择");
            [self LocalPhoto];

        }
            break;
        case 2:{

            NSLog(@"删除该照片");
            [self.currentBtn setBackgroundImage:[UIImage imageNamed:@"1111"] forState:UIControlStateNormal];

        }
            break;
        default:
            break;
    }

}

#pragma mark - 打开相机
-(void)openCamera{

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];
    } else {
        NSLog(@"无法打开相机");


    }
}


#pragma mark - 相片代理(拿相片,对相片的处理)
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    // 获取文件类型
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    // 判断是不是图片
    if ([type isEqualToString:@"public.image"]) {

        // 编辑后的图片
        //备注 : info[@"UIImagePickerControllerEditedImage"] 是编辑后的图片
            [self.currentBtn setBackgroundImage:info[@"UIImagePickerControllerEditedImage"] forState:UIControlStateNormal];

        // 关闭相册
        [picker dismissViewControllerAnimated:YES completion:nil];

    }
}

#pragma mark - 压缩图片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);

    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    // End the context
    UIGraphicsEndImageContext();

    // Return the new image.
    return newImage;
}

不对的地方请大神指正。

你可能感兴趣的:(iOS)