gpt4 book ai didi

ios - 解析加载图像到 UIImageView iOS

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

这是我传递给场景的图像数据的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
rowNo = indexPath.row;
PFUser *currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"Photo"];
[query whereKey:@"username" equalTo:currentUser.username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
_photo = [objects objectAtIndex:rowNo];
}];

[self performSegueWithIdentifier:@"showImageCloseup" sender:self];

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
ImageCloseupViewController *destination = [segue destinationViewController];
destination.photoObject = _photo;
}

这是加载图像的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
PFFile *p = [_photoObject objectForKey:@"photo"];
[p getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if(!error){
UIImage *image = [UIImage imageWithData:data];
[_imageView setImage:image];
}
}];

由于某种原因,图片没有加载,这是为什么?我该如何解决?

最佳答案

这个:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
_photo = [objects objectAtIndex:rowNo];
}];

[self performSegueWithIdentifier:@"showImageCloseup" sender:self];

需要:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
_photo = [objects objectAtIndex:rowNo];
[self performSegueWithIdentifier:@"showImageCloseup" sender:self];
}];

因为:

// Runs 1st - executes in background thread stores block, then runs block once it's done
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// Runs 3rd - already segued at this point
_photo = [objects objectAtIndex:rowNo];
}];

// Runs 2nd - starts segue
[self performSegueWithIdentifier:@"showImageCloseup" sender:self];

但是,您的设计模式似乎存在整体问题。如果您已经可以访问对象,则不必每次都重新查询整个数据库。您是否有对填充 tableView 的数组的引用?如果是这样,像这样:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
rowNo = indexPath.row;
_photo = yourDataArray[indexPath.row];
[self performSegueWithIdentifier:@"showImageCloseup" sender:self];

}

关于ios - 解析加载图像到 UIImageView iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22950089/

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