Jsonの配列URL画像があります。
アプリ内で画像をダウンロード、保存、表示したい。アプリに画像が存在することを確認してください。
手伝って頂けますか!
少し早いですがお礼を
コードのダウンロード、保存、表示:->チェック画像が存在するかどうかわかりません使用:
for (int i=0; i<[self.imageURLs count]; i++) {
if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"img%d.jpg",i]]) {
NSLog(@"file exists at the path");
}
else
NSLog(@"file doesnt exist");
int y_lblLogo = i==0 ? 45 : (20 * (i+1)) - 15 ;
UIImage * imageFromURL = [self getImageFromURL:[self.imageURLs objectAtIndex:i]];
[self saveImage:imageFromURL withFileName:[NSString stringWithFormat:@"img%d",i] ofType:@"png" inDirectory:documentsDirectoryPath];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(200 * i, 0, 100, 100)];
UIImage * imageFromWeb = [self loadImage:[NSString stringWithFormat:@"img%d",i] ofType:@"png" inDirectory:documentsDirectoryPath];
imgView.image = imageFromWeb;
[imgView drawRect:CGRectMake(0,y_lblLogo,70.f,30.f)];
//[self.view addSubview:imgView];
}
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@",
imageName, @"png"]] options:NSAtomicWrite error:nil];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString
stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite
error:nil];
} else {
}
}
-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];
return result;
}