给定一个表示照片的ALAsset,是否可以在不将图像加载到UIImageView和不使用aspectRationThumnail方法的情况下检索照片的大小(高度和宽度)?
发布于 2012-04-10 18:39:03
只需注意: iOS 5.1为ALAssetRepresentation实例引入了一个新的属性维度。这将返回一个具有原始图像尺寸的CGSize结构,并且可能是将来解决此问题的最佳解决方案。
干杯,
亨德里克
发布于 2014-06-16 21:18:12
float width = asset.defaultRepresentation.dimensions.width;
float height = asset.defaultRepresentation.dimensions.height;
它快速,稳定,并给出了实际尺寸。我已经把它用在视频的ALAsset上了。
发布于 2012-04-09 10:30:33
更新
正如评论中指出的那样,这并没有像最初提供的那样工作。我已经修复了它,但它现在加载所有的图像数据,而这是操作试图避免的。它仍然避免了额外的、更糟糕的步骤,即将数据解压缩成图像。
defaultRepresentation
of ALAsset。下面的代码代表了上面的步骤。
// This method requires the ImageIO.framework
// This requires memory for the size of the image in bytes, but does not decompress it.
- (CGSize)sizeOfImageWithData:(NSData*) data;
{
CGSize imageSize = CGSizeZero;
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);
if (source)
{
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImageSourceShouldCache];
NSDictionary *properties = (__bridge_transfer NSDictionary*) CGImageSourceCopyPropertiesAtIndex(source, 0, (__bridge CFDictionaryRef) options);
if (properties)
{
NSNumber *width = [properties objectForKey:(NSString *)kCGImagePropertyPixelWidth];
NSNumber *height = [properties objectForKey:(NSString *)kCGImagePropertyPixelHeight];
if ((width != nil) && (height != nil))
imageSize = CGSizeMake(width.floatValue, height.floatValue);
}
CFRelease(source);
}
return imageSize;
}
- (CGSize)sizeOfAssetRepresentation:(ALAssetRepresentation*) assetRepresentation;
{
// It may be more efficient to read the [[[assetRepresentation] metadata] objectForKey:@"PixelWidth"] integerValue] and corresponding height instead.
// Read all the bytes for the image into NSData.
long long imageDataSize = [assetRepresentation size];
uint8_t* imageDataBytes = malloc(imageDataSize);
[assetRepresentation getBytes:imageDataBytes fromOffset:0 length:imageDataSize error:nil];
NSData *data = [NSData dataWithBytesNoCopy:imageDataBytes length:imageDataSize freeWhenDone:YES];
return [self sizeOfImageWithData:data];
}
- (CGSize)sizeOfAsset:(ALAsset*) asset;
{
return [self sizeOfAssetRepresentation:[asset defaultRepresentation]];
}
https://stackoverflow.com/questions/10067765
复制相似问题