我正在尝试从服务器下载一些图像,然后将这些图像设置为某些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];
但没有任何反应!!,如何检查这些图像是否在目录中以避免更多下载
如果你帮我解决这个问题,我将不胜感激。
谢谢!EDITED @Fernando Cervantes
我创建了一个方法来设置这样的按钮BG图像,但我不知道为什么不起作用!
- (UIImage *)loadImages :(NSString *)fileNames ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
[[NSFileManager defaultManager] fileExistsAtPath:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileNames, extension]]];
return 0;
}
并设置BG图像:
- (void)buttonsBGImage {
UIImage * bgMag2 = [self loadImages:@"i224_mag2" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
[mag2 setBackgroundImage:bgMag2 forState:UIControlStateNormal];
}
但实际上没有任何反应!即使我检查文件,它在应用程序目录
答案 0 :(得分:1)
[UIImage imageNamed:@"i224_mag2.png"]; // it is only for bundle. You should use
[UIImage imageWithContentsOfFile:path]
或
[UIImage imageWithData:data];
更新
我使用UIButton类的类别。
- (void)loadImage:(NSString *)URL withActivityIndicator:(BOOL)hasActivity refreshObject:(UIView *)refresh{
// load image from cache cutted
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{
NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:URL]];
//this will set the image when loading is finished
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *loadedImage = [UIImage imageWithData:image];
// add image to cache cutted
[self setImage:loadedImage forState:UIControlStateNormal];
});
});
}
答案 1 :(得分:1)
以下方法可能对您有所帮助。
保存强>
-(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;
}
答案 2 :(得分:0)
您正在使用dataWithContentsOfFile:
API创建NSData。你应该使用dataWithContentsOfURL
代替。
NSString *urlMag1 = [NSString stringWithFormat:@"http://myweb.com/i224_mag1.png"];
NSString *urlMag2 = [NSString stringWithFormat:@"http://myweb.com/i224_mag2.png"];
NSData *image1 = [NSdata contentsOfURL:[NSURL URLWithString:urlMag1]];
NSData *image2 = [NSdata contentsOfURL:[NSURL URLWithString:urlMag2]];
[mag2 setBackgroundImage:imgMag1 forState:UIControlStateNormal];
通常[NSdata contentsOfURL:[NSURL URLWithString:urlMag1]];
将冻结UI,直到图像下载完成。