0

私は、iPhone ギャラリーにある写真を参照して表示する必要があるプロジェクトに取り組んでいます。

そのために ALAssetLibrary を使用します。サムネイル画像を生成するには、「aspectRatioThumbnail」を使用します。私の 3GS iOS 5 ではすべてがうまくスムーズに動作します。しかし、これらの方法は iOS4 には存在しません。

この関数を使用してサムネイル比率の画像を手動で生成しようとしましたが、メモリ警告のためにクラッシュしました。ログは、生成された画像サイズが最大サイズ 120 の指定された制約を尊重していないことを示しています。

何か案は ?

    NSDate* firstDate = [NSDate date];
    uint8_t* buffer = (Byte*)malloc(_asset.defaultRepresentation.size);

    NSUInteger length = [_asset.defaultRepresentation getBytes:buffer fromOffset:0.0 length:_asset.defaultRepresentation.size error:nil];

NSData* data = [[NSData alloc] initWithBytesNoCopy:buffer length:_asset.defaultRepresentation.size freeWhenDone:YES];
                                  nil];

        NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
                           (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                           (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                           (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                           [NSNumber numberWithInt:120], kCGImageSourceThumbnailMaxPixelSize,
                           nil];

        CGImageSourceRef sourceRef = CGImageSourceCreateWithData((CFDataRef)data, (CFDictionaryRef) options);
        CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(sourceRef, 0, NULL);

        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, imageProperties);

        image = [UIImage imageWithCGImage:imageRef];

        NSTimeInterval tic = [[NSDate date] timeIntervalSinceDate:firstDate];
        NSLog(@"Thumbnail generation from ios4 method %f [size %f * %f", tic, image.size.width, image.size.height);

        [data release];
        CFRelease(sourceRef);
        CFRelease(imageRef);
        CFRelease(imageProperties);
4

1 に答える 1

2

上記のコードには、不要なコピーがたくさんあります。代わりに、画面解像度(フル画像サイズよりも小さい)で画像をロードし、そこからサムネイルにダウンサイズします。

CGImageRef img = [[asset defaultRepresentation] fullScreenImage];
int w = CGImageGetWidth(img);
int h = CGImageGetHeight(img);
float scale = 120 / (float)(MAX(w, h));
CGSize newsize = CGSizeMake(w * scale, h * scale);

UIGraphicsBeginImageContext(newsize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh);
CGContextTranslateCTM(ctx, 0, newsize.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx, CGRectMake(0, 0, newsize.width, newsize.height), iref);
UIImage* thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

このコードを実行すると、サムネイルには、aspectRatioThumbnailと同等のUIImageが作成されます。

于 2012-12-03T01:28:25.720 に答える