Objective c 使用AFN网络设置UIButton映像
我的项目中有这样一段代码,用于创建带有标签的UIButtons和带有来自web服务的值的图像Objective c 使用AFN网络设置UIButton映像,objective-c,tags,uibutton,uiimage,afnetworking,Objective C,Tags,Uibutton,Uiimage,Afnetworking,我的项目中有这样一段代码,用于创建带有标签的UIButtons和带有来自web服务的值的图像 - (void)getMoviePosterImages { for (int i = 0; i < [self.rowCount intValue]; i++) { NSArray *postersArray = [self.jsonMutableArray objectAtIndex:i]; AFImageRequestOperation *op
- (void)getMoviePosterImages
{
for (int i = 0; i < [self.rowCount intValue]; i++)
{
NSArray *postersArray = [self.jsonMutableArray objectAtIndex:i];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[moviePosterButton setBackgroundImage:image forState:UIControlStateNormal];
}
failure:nil];
[operation start];
}
}
-(无效)获取电影后期图像
{
for(int i=0;i<[self.rowCount intValue];i++)
{
NSArray*postersArray=[self.jsonMutableArray objectAtIndex:i];
AFImageRequestOperation*operation=[AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]imageProcessingBlock:nil
成功:^(NSURLRequest*请求,NSHTTPURLResponse*响应,UIImage*图像)
{
[moviePosterButton setBackgroundImage:image for状态:uicontrol状态正常];
}
失败:无];
[操作启动];
}
}
以下是我创建UILabels的代码:
- (void)displayMovieTitlesAndFrames
{
int imageCount = [self.rowCount intValue];
int offset_x = 21;
int offset_y = 24;
for (int i = 0; i < imageCount; i++)
{
// compute x & y offset
if (i % 3 == 0)
{
offset_x = 21;
}
else if (i % 3 == 1)
{
offset_x = 115;
}
else
{
offset_x = 210;
}
whiteBackgroundLabel = [[[UILabel alloc] initWithFrame:CGRectMake(offset_x, offset_y, MOVIE_THUMBNAIL_W, MOVIE_THUMBNAIL_H)] autorelease];
whiteBackgroundLabel.backgroundColor = [UIColor whiteColor];
whiteBackgroundLabel.layer.cornerRadius = 3.0;
whiteBackgroundLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
whiteBackgroundLabel.layer.borderWidth = 1.0;
[scrollview addSubview:whiteBackgroundLabel];
moviePosterButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
moviePosterButton.frame = CGRectMake(offset_x + 5, offset_y + 5, MOVIE_THUMBNAIL_W - 10, MOVIE_THUMBNAIL_H - 10);
moviePosterButton.backgroundColor = [UIColor clearColor];
[moviePosterButton setBackgroundImage:[UIImage imageNamed:@"placeholder.png"] forState:UIControlStateNormal];
moviePosterButton.tag = i;
[moviePosterButton addTarget:self
action:@selector(imageButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:moviePosterButton];
movieTitleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(offset_x, offset_y + 143, 90, 20)] autorelease];
movieTitleLabel.backgroundColor = [UIColor whiteColor];
movieTitleLabel.layer.cornerRadius = 3.0;
movieTitleLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
movieTitleLabel.layer.borderWidth = 1.0;
movieTitleLabel.font = [UIFont fontWithName:@"Helvetica" size:11.0];
movieTitleLabel.textColor = [UIColor darkGrayColor];
movieTitleLabel.textAlignment = UITextAlignmentCenter;
movieTitleLabel.numberOfLines = 1;
movieTitleLabel.text = [[self.jsonMutableArray objectAtIndex:i] objectAtIndex:1];
[scrollview addSubview:movieTitleLabel];
quickBuyButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
quickBuyButton.frame = CGRectMake(offset_x + 1 + (MOVIE_THUMBNAIL_W - QUICK_BUY_BUTTON_W - 2), offset_y - 1 + 2, QUICK_BUY_BUTTON_W, QUICK_BUY_BUTTON_H);
quickBuyButton.backgroundColor = [UIColor clearColor];
[quickBuyButton setBackgroundImage:QUICK_BUY_BUTTON forState:UIControlStateNormal];
quickBuyButton.tag = i;
[quickBuyButton addTarget:self
action:@selector(quickBuyButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:quickBuyButton];
// increment offset
if (offset_x == 210)
{
offset_y += 171;
}
}
if ((offset_x == 21) || (offset_x == 115))
{
offset_y += 171;
}
[self adjustScrollViewAndBackground:offset_y];
}
-(无效)显示电影字幕和帧
{
int imageCount=[self.rowCount intValue];
int offset_x=21;
int offset_y=24;
对于(int i=0;i
我所有的UIButton都是用它们各自的标签创建的,我想把它们的图像放在每个按钮上。上面的代码仅适用于最后创建的UIButton。我想使用上面的代码用图像填充我的所有按钮。有人能告诉我哪里弄错了吗?在您的
GetMoviePosterImage
方法中,您正在将图像设置为moviePosterButton
,它可能是您刚刚创建的最后一个按钮,因此如果只有最后一个按钮获得图像,这是正常的。您需要通过标签检索每个按钮,或者使用所有按钮构建一个数组,以便轻松访问它们。你可以这样做:
- (void)getMoviePosterImages
{
for (int i = 0; i < [self.rowCount intValue]; i++)
{
NSArray *postersArray = [self.jsonMutableArray objectAtIndex:i];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[(UIButton *)buttonsArray[i] setBackgroundImage:image forState:UIControlStateNormal];
// or
// [(UIButton *)[self.view viewWithTag:i] setBackgroundImage:image forState:UIControlStateNormal];
}
failure:nil];
[operation start];
}
}
-(无效)获取电影后期图像
{
for(int i=0;i<[self.rowCount intValue];i++)
{
NSArray*postersArray=[self.jsonMutableArray objectAtIndex:i];
AFImageRequestOperation*operation=[AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequestWithURL:[NSURL URLWithString:[postersArray objectAtIndex:6]]imageProcessingBlock:nil
成功:^(NSURLRequest*请求,NSHTTPURLResponse*响应,UIImage*图像)
{
[(UIButton*)按钮阵列[i]设置背景图像:状态图像:UIControlStateNormal];
//或
//[(UIButton*)[self.view view with tag:i]setBackgroundImage:image for State:UIControlStateNormal];
}
失败:无];
[操作启动];
}
}
希望这有帮助;) 我的按钮在滚动视图中。我如何实现它<代码>自我。滚动视图不起作用。只需使用
滚动视图
。我还注意到moviePosterButton.tag和quickBuyButton.tag有相同的标签,这不好,没关系。quickBuyButton只是一个普通的图像。