根据url缓存视频封面图、图片压缩图

一、对于图片:

  • 图片质量压缩:NSData *newData = UIImageJPEGRepresentation(image, 0.01);
  • 把得到的NSData通过SDImageCache缓存到起来
  • 获取的时候,从缓存中获取
这里建议缓存NSData

如下图:是缓存的同一张图,大小是不一样的,因为NSData的大小不等于UIImage的大小,压缩过后把NSData使用imageWithData:转成UIImage后在缓存会增大压缩图大小。

+ (UIImage *)getThumbnailInImage:(NSString *)imagURL{
    
    //先从缓存中找是否有图片
    SDImageCache *cache =  [SDImageCache sharedImageCache];
    UIImage *memoryImage =  [cache imageFromMemoryCacheForKey:imagURL];
    if (memoryImage) {
        return memoryImage;
    }else{
        UIImage *diskImage =  [cache imageFromDiskCacheForKey:imagURL];
        if (diskImage) {
            return diskImage;
        }
    }
    
    if (imagURL && [imagURL containsString:@"http"]) {
        
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imagURL]];
        
        if (!imageData) {
            UIImage *image = [UIImage imageNamed:@"default"];
            return image;
        }
        
        UIImage *image = [UIImage imageWithData:imageData];
        //图片压缩,制作缩略图
        NSData *newData = UIImageJPEGRepresentation(image, 0.01);
        //先缓存,在取图
        [[SDImageCache sharedImageCache] storeImageDataToDisk:newData forKey:imagURL];
        UIImage *newImg =  [cache imageFromDiskCacheForKey:imagURL];
        if (newImg) {
            return newImg;
        }else{
            return [UIImage imageWithData:newData];
        }
    }else{
        UIImage *image = [UIImage imageNamed:@"default"];
        return image;
    }
}

二、对于视频

获取视频的第一帧 返回图片

//获取视频的第一帧 返回图片
+ (UIImage *)getImage:(NSString *)videoURL{
    
    //先从缓存中找是否有图片
    SDImageCache *cache =  [SDImageCache sharedImageCache];
    UIImage *memoryImage =  [cache imageFromMemoryCacheForKey:videoURL];
    if (memoryImage) {
        return memoryImage;
    }else{
        UIImage *diskImage =  [cache imageFromDiskCacheForKey:videoURL];
        if (diskImage) {
            return diskImage;
        }
    }

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:videoURL] options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = 1;
    NSError *thumbnailImageGenerationError = nil;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];

    UIImage *thumbnailImage = nil;
    if (thumbnailImageRef) {
        
        thumbnailImage = [[UIImage alloc]initWithCGImage: thumbnailImageRef];
        [[SDImageCache sharedImageCache] storeImage:thumbnailImage forKey:videoURL toDisk:YES];
        return thumbnailImage;
    }else{
        thumbnailImage = [UIImage imageNamed:@"default"];
        return thumbnailImage;
    }
}

你可能感兴趣的:(根据url缓存视频封面图、图片压缩图)