ios - 将按钮图像设置为从 xcode 库中选择的图像

标签 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];
}
}

任何帮助将不胜感激!

谢谢

最佳答案

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

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

完成。

关于ios - 将按钮图像设置为从 xcode 库中选择的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15955398/

相关文章:

ios - Swift 窗口 View 顶部的 subview

ios - 方法 'fontWithName(_:size:)' 与 Objective-C 选择器冲突

Python imread 错误 : "Unsupported BMP bitfields layout"

python - 随机点立体图的抗锯齿

ios - 设计问题——我应该使用什么技术在我的应用程序中向另一个用户发送文本?

ios - 如何从当前选项卡栏加载第二个选项卡栏

iOS ViewController 边界使用与 Xcode Interface Builder 中的设备相同的边界

xcode - 如何更改 xcode 中快捷方式生成的注释样式?

ios - 带图像的推送通知 - iOS - Swift

ios - 异常说发送到一个不符合 "frame"属性的 KVC 的对象