3

我正在将 png 图像作为 NSData (用于缓存)保存到磁盘,当读取时以黑色代替透明像素。如何解决?

这是我用来保存到磁盘的:

-(void)cacheImage:(UIImage *)image forURL:(NSURL *)url {
NSURL *cacheFileURL = [[self asyncableCachesDirectory] URLByAppendingPathComponent:[[url absoluteString] MD5]];

NSData *imageData = nil;

switch ([self imageTypeForJTDynamicImageURL:url]) {
    case AsyncableImageTypeJPEG:
        imageData = UIImageJPEGRepresentation(image, 1);
        break;
    case AsyncableImageTypePNG:
        imageData = UIImagePNGRepresentation(image);
        break;
    default:
        break;
}

NSError *error = nil;

[imageData writeToURL:cacheFileURL options:0 error:&error];

}

这就是我用来阅读图像的内容:

-(UIImage *)imageFromCacheForURL:(NSURL *)url {
UIImage *image;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *urlHash;
NSURL *cacheFileURL;


image = [self.images objectForKey:[url absoluteString]];

// if we already have the full size image in memory just return it.
if (image) {
    debug(@"---Asyncable returning full size image from memory for url: %@", url);
    return image;
}

// check disk for full size image
urlHash = [[url absoluteString] MD5];
cacheFileURL = [[self asyncableCachesDirectory] URLByAppendingPathComponent:urlHash];

image = [UIImage imageWithData:[fileManager contentsAtPath:[cacheFileURL path]]];

if (image) {
    [self.images setObject:images forKey:url];
    debug(@"---Asyncable returning full size image from disk for url: %@", url);

    return image;
}

}

4

1 回答 1

0
imageData = UIImagePNGRepresentation(image);

如果在这两种情况下使用会发生什么?

于 2012-05-18T09:13:06.087 回答