gpt4 book ai didi

ios - 循环添加 UIImageView Cocoa Touch

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:02 24 4
gpt4 key购买 nike

向我的 View 添加多个 ImageView 时出现问题。我有一系列图像以及要在它们之间循环并添加到我的 View 中的内容。此代码段添加了要查看的图像,但没有框架!

- (void)cycleImages:(NSArray *)images {
//create an image view

int fromLeft = 5;
int profilesCount = [images count] - 1;

for(int n = 0; n <= profilesCount; n++){



//add image view to view
[self.view addSubview:[[[UIImageView alloc] initWithFrame:CGRectMake(5, fromLeft, 48, 48)] initWithImage:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:n]]]]]];

fromLeft = fromLeft + 53;

NSLog(@"%d", fromLeft);
}
}

感谢您的帮助!

敏捷

最佳答案

很难阅读这样的代码,并且存在内存泄漏。请试试这个。

- (void)cycleImages:(NSArray *)images 
{
int fromLeft = 5;
NSURL* url = nil;
UIImage* img = nil;
UIImageView* imgView = nil;
NSData* imgData = nil;

for (NSString* imgURLStr in images)//Using enumeration much more easy and in ObjC style
{
//Put breakpoint here and check all varibles initialized coorecly step by step
url = [NSURL URLWithString:imgURLStr];
imgData = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:imgData];
imgView = [[UIImageView alloc] initWithImage:img];

imgView.frame = CGRectMake(5, fromLeft, 48, 48);

[self.view addSubView:imgView];

fromLeft = fromLeft + 53;
NSLog(@"%d", fromLeft);

//Clean up
[img release];
[imgView release];
}
}

谢谢!

关于ios - 循环添加 UIImageView Cocoa Touch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856505/

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