0

我必须从使用此代码获得的文件中调整专辑插图的大小:

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/

如何获得分辨率更高的图像?

4

2 回答 2

0

尝试使用以下方法明确指定上下文的插值级别:

CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);

您的 resizeImage:width:height: 方法因此变为:

-(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);
   CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh);
   CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
   CGImageRef ref = CGBitmapContextCreateImage(bitmap);
   UIImage *result = [UIImage imageWithCGImage:ref];

   CGContextRelease(bitmap);
   CGImageRelease(ref);

   return result;

}

于 2013-01-13T17:44:11.790 回答
0

如果您的视图是 74x74,您应该在 Retina 显示器上调整为两倍。所以,像:

CGFloat imageSize = 74.0f * [[UIScreen mainScreen] scale];
UIImage *newImage = [self resizeImage:img width:imageSize height:imageSize];

然后你需要将你的图像视图的 contentMode 设置为 UIViewContentModeScaleAspectFill 之类的东西。

于 2013-01-13T18:58:53.197 回答