Iphone 设置图像大小

Iphone 设置图像大小,iphone,objective-c,ios,cocoa-touch,alassetslibrary,Iphone,Objective C,Ios,Cocoa Touch,Alassetslibrary,给定一个表示照片的集合,是否可以在不将图像加载到UIImageView且不使用aspectRationThumnail方法的情况下检索照片的大小(高度和宽度) 正如评论中所指出的,这并没有像最初提供的那样起作用。我已经修复了它,但它现在加载了所有的图像数据,而OP正试图避免这些数据。它仍然避免了将数据解压缩到图像中这一额外的、更糟糕的步骤 获取集合的defaultRepresentation 获取表示形式的数据 使用功能的自适应。谢谢你,沙帕科夫斯基 下面的代码表示上述步骤 // This me

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

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

  • 获取集合的
    defaultRepresentation
  • 获取表示形式的数据
  • 使用功能的自适应。谢谢你,沙帕科夫斯基
  • 下面的代码表示上述步骤

    // 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]];
    }
    

    访问图像大小的更简单方法是通过
    [ALAssetRepresentation metadata]
    。在我测试的图像上,这个
    NSDictionary
    包含名为
    PixelWidth
    PixelHeight
    的键,它们是
    NSNumber
    对象,具有您期望的值

    但是,对于您将找到的确切密钥似乎没有特别的保证,因此请确保您的应用程序能够处理这些密钥不在元数据中的情况。有关速度和线程安全的一些注意事项,请参见

    比较 我在iPad的整个资产库中测试了这两种方法——在CGImageSourceRef中加载图像数据或读取元数据。这两种方法在FLT_EPSILON中返回相同的大小。除了2个花费双倍时间的异常值外,16次重复的运行时间非常相似:

    Method | Mean time +/- 95% confidence Size from CGImageSourceRef | 0.1787 +/- 0.0004 Size from metadata | 0.1789 +/- 0.0015 方法|平均时间+/-95%置信度 CGImageSourceRef的尺寸| 0.1787+/-0.0004 元数据的大小| 0.1789+/-0.0015 因此,这两种方法都没有性能优势。完全有可能通过读取图像数据按需构建元数据字典。

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

    干杯

    亨德里克

    float width = CGImageGetWidth(asset.defaultRepresentation.fullResolutionImage);
    float height = CGImageGetHeight(asset.defaultRepresentation.fullResolutionImage);
    
    或与
    资产.defaultRepresentation.fullScreenImage
    相同

    float width = asset.defaultRepresentation.dimensions.width;
    float height = asset.defaultRepresentation.dimensions.height;
    

    它快速、稳定,并给出实际尺寸。我用过一套视频

    谢谢你花时间把它挂起来。在执行此操作时,由于某些原因,获取ImageIO:CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource失败,错误代码为-11。不知道ALAsset URL是否与此兼容?@CoDEFRo:是的,我恐怕没有实际测试ALAsset对象的NSURL步骤。似乎无法从URL加载数据,所以我不确定您应该如何处理它。我编辑了一些代码,提供了一些有用的代码,还提出了另一个答案。我认为sizeOfImageWithData方法不正确-在我的测试中,我还需要查看方向标志。在纵向模式下拍摄图像时,会反转宽度/高度,方向值为6(镜像纵向模式)。无论如何,我认为它还需要考虑KCGimageProperty和方向。是的,这比我建议的任何一种方法都快得多。我可以问一下
    dimensons
    属性是如何被发现的,因为即使对于iOS 5.1,ALAssetRepresentation类参考中也没有提到它。API差异:在大图像上效果很差