Xcode从相机设置按钮图像

时间:2014-08-09 23:13:26

标签: ios uibutton

我有两个与按钮图像有关的问题:

  1. 设置按钮图像

  2. 将图像更改为圆形图像

  3. 我有一个iPhone应用程序,它在Storyboard中创建了一个UIButton。使用Attributes Inspector,我将按钮图像设置为我创建并添加到项目中的图像。

    该按钮将允许用户添加个人资料照片。

    当我点击按钮时,我要求用户使用手机相机拍摄图像或从相机胶卷中选择图像。

    这一切都有效,我可以访问相机拍照和相机胶卷图库以选择图像。

    我的问题是: 1。 如何将按钮图像设置为用户选择的图像? 在我的didFinishPickingMediaWithInfo方法中,我有:

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    // Set the image of the Button to the selected image
    [sender setImage:image forState:UIControlStateNormal];
    

    但我收到错误“使用未声明的标识符” - 因为我的按钮不是发件人。

    如何将我的按钮图像设置为相机或相机胶卷图像中的图像?如果我以编程方式创建按钮,我可以设置图像,但如果按钮是在Storyboard中创建的,那么似乎需要做很多不必要的工作? 或者我是否需要以编程方式创建按钮才能执行此操作?

    2。 我尝试在我的didFinishPickingMediaWithInfo中屏蔽图像以使其具有圆形外观,但我无法这样做。我该怎么做呢?我可以通过在viewDidLoad中调整下面的代码来使图像四舍五入,但它在我的didFinishPickingMediaWithInfo中不起作用

    self.image.layer.cornerRadius = self.profileImageView.frame.size.width / 2;
    self.image.clipsToBounds = YES;
    

2 个答案:

答案 0 :(得分:3)

以下是一个例子:

#import "MyViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface MyViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property (weak, nonatomic) IBOutlet UIButton *profilePictureButton;

@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // make the button a circle.
    _profilePictureButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    _profilePictureButton.imageView.layer.cornerRadius = _profilePictureButton.frame.size.width / 2.f;
}

- (IBAction)takePicture:(id)sender
{
    UIImagePickerController *imagePickerController = [UIImagePickerController new];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController
                       animated:YES
                     completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [_profilePictureButton setImage:image forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:YES
                               completion:nil];
}

@end

您必须确保按钮的类型为UIButtonTypeCustom,否则根本无法使用。

答案 1 :(得分:1)

确保将UIButton的IBOutlet设置为cameraButton,并确保将UIButton设置为“自定义”类型。

使视图控制器符合

@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

将您的UIButton的TouchUpInside事件绑定到以下IBAction:

-(IBAction)showCameraAction:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];
}

在视图控制器中,放置以下委托实现方法。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    [self.cameraButton setImage:chosenImage forState:UIControlStateNormal];

    self.cameraButton.clipsToBounds = YES;
    self.cameraButton.layer.cornerRadius = (self.cameraButton.frame.size.width / 2);//half of the width
    self.cameraButton.layer.borderColor=[UIColor blackColor].CGColor;
    self.cameraButton.layer.borderWidth=1.0f;

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

我做了所有这些,并最终得到了一个可以点亮的按钮,可以调出相机,拍完照片后,会显示一个带有黑色边框的图像的圆形遮罩。通过将borderWidth设置为0,可以随意删除边框。