TIUITool.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // TIUITool.m
  3. // TiSDKDemo
  4. //
  5. // Created by iMacA1002 on 2019/12/4.
  6. // Copyright © 2020 Tillusory Tech. All rights reserved.
  7. //
  8. #import "TIUITool.h"
  9. #import "TiSDKInterface.h"
  10. @implementation TIUITool
  11. +(void)setWriteJsonDic:(NSDictionary *)dic toPath:(NSString *)path
  12. {
  13. NSError *error = nil;
  14. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
  15. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  16. if (!jsonData || error) {
  17. NSLog(ASLocalizedString(@"JSON解码失败"));
  18. NSLog(ASLocalizedString(@"JSON文件%@ 写入失败 error-- %@"),path,error);
  19. } else {
  20. [jsonString writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
  21. if (error) {
  22. NSLog(ASLocalizedString(@"JSON文件%@ 写入失败 error-- %@"),path,error);
  23. }
  24. }
  25. }
  26. +(id)getJsonDataForPath:(NSString *)path
  27. {
  28. NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path];
  29. NSError *error;
  30. id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
  31. if (!jsonData || error) {
  32. NSLog(ASLocalizedString(@"JSON文件%@ 解码失败 error-- %@"),path,error);
  33. return nil;
  34. } else {
  35. return jsonObj;
  36. }
  37. }
  38. +(void)getImageFromeURL:(NSString *)fileURL WithFolder:(NSString *)folder downloadComplete:(void(^) (UIImage *image))completeBlock{
  39. NSString *imageName = [[fileURL componentsSeparatedByString:@"/"] lastObject];
  40. NSString *cachePaths = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
  41. NSFileManager *fileManager = [NSFileManager defaultManager];
  42. NSString *folderPath = [cachePaths stringByAppendingFormat:@"/%@",folder];
  43. NSString *imagePath = [folderPath stringByAppendingFormat:@"/%@",imageName];
  44. if ([fileManager fileExistsAtPath:imagePath])
  45. {//文件存在
  46. UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
  47. // NSLog(ASLocalizedString(@"从本地获取图片 %@"),cachePaths);
  48. completeBlock(image);
  49. }
  50. else
  51. {
  52. if (![fileManager fileExistsAtPath:folderPath]) {
  53. //创建文件夹
  54. NSError *error = nil;
  55. [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
  56. if (error) {
  57. NSLog(ASLocalizedString(@"文件夹创建失败 err %@"),error);
  58. }else{
  59. // NSLog(ASLocalizedString(@"文件夹创建成功"));
  60. }
  61. }
  62. //下载下载图片到本地
  63. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  64. NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
  65. if (data) {
  66. dispatch_async(dispatch_get_main_queue(), ^{
  67. UIImage *image = [UIImage imageWithData:data];
  68. //写入本地
  69. // NSLog(ASLocalizedString(@"图片写入本地地址 %@"),imagePath);
  70. // [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
  71. [UIImageJPEGRepresentation(image, 0.5)writeToFile:imagePath atomically:YES];
  72. completeBlock(image);
  73. });
  74. }else{
  75. NSLog(ASLocalizedString(@"图片地址URL-》%@ \n下载失败"),fileURL);
  76. }
  77. });
  78. }
  79. }
  80. @end