我必须从使用此代码获得的文件中调整专辑插图的大小:
for (NSString *format in [asset availableMetadataFormats]) {
for (AVMetadataItem *item in [asset metadataForFormat:format]) {
if ([[item commonKey] isEqualToString:@"title"]) {
//NSLog(@"name: %@", (NSString *)[item value]);
downloadedCell.nameLabel.text = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:@"artist"]) {
downloadedCell.artistLabel.text = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:@"albumName"]) {
//musicItem.strAlbumName = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:@"artwork"]) {
UIImage *img = nil;
if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) {
img = [UIImage imageWithData:[item.value copyWithZone:nil]];
}
else { // if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) {
NSData *data = [(NSDictionary *)[item value] objectForKey:@"data"];
img = [UIImage imageWithData:data];
}
// musicItem.imgArtwork = img;
UIImage *newImage = [self resizeImage:img width:70.0f height:70.0f];
downloadedCell.artworkImage.image = newImage;
}
当我应用这种方法时:
- (UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {
//NSLog(@"resizing");
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
//if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef),
4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
我总是得到一个像你在上面的照片中看到的噪声图像。 http://postimage.org/image/jltpfza11/
如何获得分辨率更高的图像?