从Json下载并保存图像
我有一个来自Json的数组url图像。
我想下载,保存和显示图像内的应用程序。 检查图像存在于应用程序。
你可以帮我吗!
预先感谢
代码下载,保存并显示: – >我不知道check image是否存在使用它:
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; }
这里是你需要的完整的代码:
- (UIImage *)imageFromURL:(NSString*) surl fromCache:(BOOL)fromCache { UIImage *img; if(surl!=nil && [surl compare:@""]!=NSOrderedSame) { NSString* key = [self getFilenameFromUrl:surl]; NSData *data = [self getDataFromURL:surl fromCache:fromCache]; if(data) { img = [[UIImage alloc] initWithData:data]; } } return img; } -(NSString*) getFilenameFromUrl:(NSString*) surl { NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^a-zA-Z0-9\\._]" options:NSRegularExpressionCaseInsensitive error:nil]; return [regex stringByReplacingMatchesInString:surl options:0 range:NSMakeRange(0, [surl length]) withTemplate:@"_"]; } -(NSString*) getCacheFolder { NSFileManager *filemgr = [NSFileManager defaultManager]; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *sDocumentsDir = [documentPaths objectAtIndex:0]; sDocumentsDir = [sDocumentsDir stringByAppendingPathComponent:@"/.tmp"]; BOOL isDir; if(!([filemgr fileExistsAtPath:sDocumentsDir isDirectory:&isDir] && isDir)) { [filemgr createDirectoryAtPath:sDocumentsDir withIntermediateDirectories:YES attributes:nil error:nil]; } return sDocumentsDir; } -(NSData *) getDataFromURL:(NSString*) surl fromCache:(BOOL) fromCache { NSData *urlData; if(surl!=nil && [surl compare:@""]!=NSOrderedSame) { surl = [surl stringByReplacingOccurrencesOfString:@" " withString:@"20"]; NSFileManager *filemgr = [NSFileManager defaultManager]; NSString *furl = [NSString stringWithFormat:@"%@/%@",[self getCacheFolder],[self getFilenameFromUrl:surl]]; if (fromCache && [filemgr fileExistsAtPath: furl]==YES) { //NSLog (@"File exists"); urlData = [filemgr contentsAtPath: furl]; } else if([self hasConnection]){ //NSLog (@"File not found"); [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; NSURL *url = [NSURL URLWithString:surl]; NSError *err; urlData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&err]; if (!err && urlData) { [filemgr createFileAtPath: furl contents: urlData attributes: nil]; } else { NSLog(@"%@",err); } [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } } return urlData; }
另外,要显示您可能会使用一些现有的组件,例如FGallery 。