gpt4 book ai didi

ios - 在 UIImageView 中显示事件指示器

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

当我从 url 下载内容时,我正尝试将事件指示器添加到 imageView 中,但问题是事件指示器不可见。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = nil;

if ([imageArray count] >0){
if(cachedImage[[imageArray objectAtIndex:indexPath.row]] != nil){
recipeImageView.image = cachedImage[[imageArray objectAtIndex:indexPath.row]];
}
else{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(140, 236, 37, 37);
[activityIndicator startAnimating];
[recipeImageView addSubview:activityIndicator];
self.view.userInteractionEnabled = NO;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];

dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
[activityIndicator stopAnimating];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
});
});
}
}

如果可能的话,我怎样才能在 imageview 中显示带有进度百分比的事件指示器?

最佳答案

只需更新您的代码并试试这个

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = nil;

if ([imageArray count] >0){
if(cachedImage[[imageArray objectAtIndex:indexPath.row]] != nil){
recipeImageView.image = cachedImage[[imageArray objectAtIndex:indexPath.row]];
}
else{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicator setCenter: recipeImageView.center];
[activityIndicator startAnimating];
[cell.contentView addSubview:activityIndicator];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];

dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
[activityIndicator removeFromSuperview];
cachedImage[[imageArray objectAtIndex:indexPath.row]] = image;
});
});
}
}

关于ios - 在 UIImageView 中显示事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38436177/

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