我想知道如何在点击按钮时显示来自URL(位于xml文件中)的图像。
答案 0 :(得分:5)
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]];
}
使用想要显示的图像.........
答案 1 :(得分:2)
UIImage *pImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];;
[imageView setImage:pImage];