gpt4 book ai didi

iphone - 下载图片和UIButton BG图片

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

我正在尝试从服务器下载一些图像,然后将这些图像设置为某些 UIButton 的背景图像。

所以首先下载图片:

NSString *urlMag1 = [NSString stringWithFormat:@"http://myweb.com/i224_mag1.png"];
NSString *urlMag2 = [NSString stringWithFormat:@"http://myweb.com/i224_mag2.png"];
NSString *str = [NSString stringWithFormat:urlMag1,urlMag2,nil];

NSData *data = [NSData dataWithContentsOfFile:str];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[data writeToFile:path atomically:YES];

UIImage *imgMag1 = [UIImage imageNamed:@"i224_mag2.png"];
[mag2 setBackgroundImage:imgMag1 forState:UIControlStateNormal];

但是没有任何反应!!,我如何检查这些图像是否在目录中以避免更多下载

如果你能帮我解决这个问题,我将不胜感激

谢谢!

已编辑@费尔南多·塞万提斯

我创建了一个方法来设置按钮 BG 图像类似这样,但我不知道为什么不起作用!

- (UIImage *)loadImages :(NSString *)fileNames ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {

[[NSFileManager defaultManager] fileExistsAtPath:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileNames, extension]]];
return 0;
}

并设置背景图片:

- (void)buttonsBGImage {



UIImage * bgMag2 = [self loadImages:@"i224_mag2" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

[mag2 setBackgroundImage:bgMag2 forState:UIControlStateNormal];


}

但实际上什么也没发生!即使我检查了文件,它也在应用程序目录中

最佳答案

以下方法可能会对您有所帮助。

保存

-(void) saveFile:(NSString *)fileName ofType:(NSString *)extension fromURL:(NSString *)fileIndexPath inDirectory:(NSString *)directoryPath {
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileIndexPath]];

[data writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension] atomically:YES];
}

检查

-(BOOL) checkIfFileExists:(NSString *)fileName ofType:(NSString *)extension inDirectoryPath:(NSString *)directoryPath {
bool result;

if ([[NSFileManager defaultManager] fileExistsAtPath:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, extension]]]) {
result = TRUE;
} else {
result = FALSE;
}

return result;
}

设置

UIButton * button = [[UIButton alloc] init];
UIImage * backgroundImage = [self loadImage:@"YourImageName" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

[button setImage:backgroundImage forState:UIControlStateNormal];

-已编辑-

我认为您的问题是您在 loadImages 方法中返回了 0。而不是返回图像本身。

我是这样实现的:

加载

-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {

UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];

return result;
}

关于iphone - 下载图片和UIButton BG图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958503/

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