美文网首页图片处理
ios 本地缓存图片

ios 本地缓存图片

作者: 行走的风车 | 来源:发表于2016-08-18 14:54 被阅读306次
    //将图片保存到本地
    + (void)SaveImageToLocal:(UIImage*)image Keys:(NSString*)key {
        NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
        //[preferences persistentDomainForName:LocalPath];
        [preferences setObject:UIImagePNGRepresentation(image) forKey:key];
    }
    
    //本地是否有相关图片
    + (BOOL)LocalHaveImage:(NSString*)key {
      NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
      //[preferences persistentDomainForName:LocalPath];
      NSData* imageData = [preferences objectForKey:key];
      if (imageData) {
          return YES;
      }
        return NO;
    }
    
    //从本地获取图片
    + (UIImage*)GetImageFromLocal:(NSString*)key {
        NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
        //[preferences persistentDomainForName:LocalPath];
        NSData* imageData = [preferences objectForKey:key];
        UIImage* image;
        if (imageData) {
            image = [UIImage imageWithData:imageData];
        }
        else {
            NSLog(@"未从本地获得图片");
        }
      return image;
    }

    相关文章

      网友评论

        本文标题:ios 本地缓存图片

        本文链接:https://www.haomeiwen.com/subject/typvsttx.html