gpt4 book ai didi

ios - 如何在iPhone中的UIScrollView中动态添加图像

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

在我的 iPhone 应用程序中,我从 Web 服务器获取了大约 300 个图像 url,我需要以 3x3 行和列格式在 UIScrollView 上显示所有图像。一旦我从网络服务器获取所有 url,将它们转换为 UIImage 并显示图像。一切工作正常,但是在加载 200 个图像后应用程序崩溃,我已经阅读了很多 UIScrollView 的内存问题,那么我们该如何解决这个问题呢?我也阅读了苹果文档,但不清楚。

最佳答案

这是解决方案,我已在我的应用程序中实现了它。这是我的代码sv_port 是我的 ScrollView 。下载 SDWebImageDownloader 类文件并将其导入到您的项目中。在您的项目中添加相关框架。像imageIO.framework、QuartzCore

现在,在您的 .m 添加

#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "SDWebImageCompat.h"

//function for adding image in your scrollview
-(void)populateImages
{
int x=6,y=6;

for(UIView * view in sv_port.subviews){
if([view isKindOfClass:[UIImageView class]])
{
[view removeFromSuperview]; view = nil;
}
if([view isKindOfClass:[UIButton class]])
{
[view removeFromSuperview]; view = nil;
}
}

for(int p=0;p<[Manufacturer count];p++)
{
UIButton *btnImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
btnImageButton.frame = CGRectMake(x,y,70,70);
[btnImageButton setTag:p];
[btnImageButton addTarget:self action:@selector(nextview:) forControlEvents:UIControlEventTouchUpInside];

UIActivityIndicatorView *spinny = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinny.frame=CGRectMake(btnImageButton.frame.origin.x+25,btnImageButton.frame.origin.y+25, 20, 20);
spinny.backgroundColor=[UIColor clearColor];
[spinny startAnimating];

[sv_port setScrollEnabled:YES];
[sv_port addSubview:spinny];

UIImageView *asyncImageView = [[[UIImageView alloc] initWithFrame:btnImageButton.frame] autorelease];
asyncImageView.backgroundColor=[UIColor clearColor];
CALayer *layer;
layer = asyncImageView.layer;
layer.borderWidth = 0.5;
layer.cornerRadius = 5;
layer.masksToBounds = YES;


[asyncImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", yourUrl] ] placeholderImage:[UIImage imageNamed:nil] options:0 andResize:CGSizeMake(btnImageButton.frame.size.width,btnImageButton.frame.size.height) withContentMode:UIViewContentModeScaleAspectFit];
asyncImageView.contentMode = UIViewContentModeScaleAspectFit;

[sv_port addSubview:btnImageButton];
[sv_port addSubview:asyncImageView];
int imgCntPort=0;

imgCntPort=(sv_port.frame.size.width/(asyncImageView.frame.size.width));

////NSLog(@"imgport %d",imgCntPort);
if((p+1)%imgCntPort==0)
{
x=5;
y=y+80;
}
else
{
x=x+80;
}
}
sv_port.contentSize = CGSizeMake(0, y);
glob_x =0; glob_y =y;
}

希望有帮助..

关于ios - 如何在iPhone中的UIScrollView中动态添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15405846/

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