gpt4 book ai didi

iphone - 如何显示来自 URL 的图像并在点击 UIButton 时显示它?

转载 作者:行者123 更新时间:2023-11-28 18:14:14 24 4
gpt4 key购买 nike

我想知道如何在点击按钮时显示来自 URL(在 xml 文件中)的图像。

最佳答案

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the size of the button
[myButton setTitle:@"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[superView addSubview:myButton];

点击按钮后的功能

-(void)buttonClicked{
//urlString should be your image url which you want to access
NSURL *url = [[NSURL alloc] initWithString:urlString ];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
}

在你想显示的地方使用图片............

关于iphone - 如何显示来自 URL 的图像并在点击 UIButton 时显示它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com