0
我正在研究需要浏览和显示iPhone画廊中存在的图片的项目。ALAsset库方面比率缩略图iOS4
我使用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);