将按钮图像设置为xcode中库的选定图像

时间:2013-04-11 17:43:59

标签: ios xcode image button

我正在创建一个应用程序,我选择一个按钮,它会引导我到库中选择一张图片。我希望所选图片成为按钮的主要图像。 有人能帮助我吗?

以下是我用于访问库的代码:

- (IBAction)image1Pressed:(id)sender {

if([UIImagePickerController isSourceTypeAvailable:
    UIImagePickerControllerSourceTypePhotoLibrary])
{
    UIImagePickerController *picker1= [[UIImagePickerController alloc]init];
    picker1.delegate = self;
    picker1.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker1 animated:YES completion:nil];
    [picker1 release];
}
}

任何帮助将不胜感激!

由于

1 个答案:

答案 0 :(得分:1)

您需要为按钮创建一个插座,我们将其命名为“imageButton”。让图像选择器完成剩下的工作:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *btnImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageButton setImage:btnImage forState:UIControlStateNormal];
}

完成。