首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ALAsset图像大小

ALAsset图像大小
EN

Stack Overflow用户
提问于 2012-04-09 08:41:19
回答 4查看 9.1K关注 0票数 4

给定一个表示照片的ALAsset,是否可以在不将图像加载到UIImageView和不使用aspectRationThumnail方法的情况下检索照片的大小(高度和宽度)?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-04-10 18:39:03

只需注意: iOS 5.1为ALAssetRepresentation实例引入了一个新的属性维度。这将返回一个具有原始图像尺寸的CGSize结构,并且可能是将来解决此问题的最佳解决方案。

干杯,

亨德里克

票数 14
EN

Stack Overflow用户

发布于 2014-06-16 21:18:12

代码语言:javascript
运行
复制
float width = asset.defaultRepresentation.dimensions.width;
float height = asset.defaultRepresentation.dimensions.height;

它快速,稳定,并给出了实际尺寸。我已经把它用在视频的ALAsset上了。

票数 6
EN

Stack Overflow用户

发布于 2012-04-09 10:30:33

更新

正如评论中指出的那样,这并没有像最初提供的那样工作。我已经修复了它,但它现在加载所有的图像数据,而这是操作试图避免的。它仍然避免了额外的、更糟糕的步骤,即将数据解压缩成图像。

  1. 获取defaultRepresentation of ALAsset。
  2. 获取ALAssetRepresentation.
  3. Use的数据,这是this handy sizeOfImageAtURL函数的改编。谢谢你,shpakovski,

下面的代码代表了上面的步骤。

代码语言:javascript
运行
复制
// 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]];
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10067765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档