Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios5 ALAssetLibrary aspectRatioThumbnail iOS4_Ios5_Ios4_Thumbnails_Alassetslibrary - Fatal编程技术网

Ios5 ALAssetLibrary aspectRatioThumbnail iOS4

Ios5 ALAssetLibrary aspectRatioThumbnail iOS4,ios5,ios4,thumbnails,alassetslibrary,Ios5,Ios4,Thumbnails,Alassetslibrary,我正在做一个项目,需要浏览和显示iPhone图库中的图片 我用图书馆来做这件事。要生成缩略图,我使用“aspectRatioThumbnail”。在我的3GS iOS 5上,一切都很顺利。但这些方法在iOS4中不存在 我尝试使用此函数手动生成缩略图比例图像,但由于内存警告而崩溃。日志显示生成的图像大小不符合给定的最大大小120的约束 有什么想法吗 NSDate* firstDate = [NSDate date]; uint8_t* buffer = (Byte*)malloc(

我正在做一个项目,需要浏览和显示iPhone图库中的图片

我用图书馆来做这件事。要生成缩略图,我使用“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);

上面的代码有很多不必要的复制。相反,只需以屏幕分辨率(小于完整图像大小)加载图像,然后从那里缩小到缩略图:

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