Ios 使用ALAssetRepresentation getBytes:fromOffset:length:error:但方向正确

Ios 使用ALAssetRepresentation getBytes:fromOffset:length:error:但方向正确,ios,objective-c,alassetslibrary,Ios,Objective C,Alassetslibrary,以下是我在iOS应用程序中尝试执行的高级操作: 允许用户从其照片库中选择资源 共享一个URL,我的应用程序外部的实体可以使用该URL请求相同的资产 我的应用程序正在运行一个Web服务器(CocoaHTTPServer),它能够向请求实体提供数据 我特意使用ALAssetRepresentation getBytes:fromOffset:length:error:实现了这一点,认为可以避免以下问题: 在提供映像之前写入映像的本地副本(然后必须管理本地副本) 将所有图像数据放入内存 性能可能会变

以下是我在iOS应用程序中尝试执行的高级操作:

  • 允许用户从其照片库中选择资源
  • 共享一个URL,我的应用程序外部的实体可以使用该URL请求相同的资产
  • 我的应用程序正在运行一个Web服务器(CocoaHTTPServer),它能够向请求实体提供数据
  • 我特意使用ALAssetRepresentation getBytes:fromOffset:length:error:实现了这一点,认为可以避免以下问题:
    • 在提供映像之前写入映像的本地副本(然后必须管理本地副本)
    • 将所有图像数据放入内存
    • 性能可能会变慢,在提供任何映像之前都要等待整个映像
  • 它的工作水平很高,但有一个问题。在某些情况下,图像的方向不正确

    这是一个众所周知的问题,但解决方案通常似乎需要从ALAssetRepresentation创建CGImage或UIImage。一旦我这样做了,它就消除了我使用方便的ALAssetRepresentation getBytes:fromOffset:length:error:method的能力

    如果有办法继续使用这种方法,但方向正确,那就太酷了。如果不是的话,我希望能就下一个最佳方法提出任何建议。谢谢

    以下是一些相关的方法:

    - (id)initWithAssetURL:(NSURL *)assetURL forConnection:(MyHTTPConnection *)parent{
    if((self = [super init]))
    {
        HTTPLogTrace();
    
        // Need this now so we can use it throughout the class
        self.connection = parent;
        self.assetURL = assetURL;
    
        offset = 0;
    
        // Grab a pointer to the ALAssetsLibrary which we can persistently use
        self.lib = [[ALAssetsLibrary alloc] init];
    
        // Really need to know the size of the asset, but we will also set a property for the ALAssetRepresentation to use later
        // Otherwise we would have to asynchronously look up the asset again by assetForURL
        [self.lib assetForURL:assetURL
             resultBlock:^(ALAsset *asset) {
                 self.assetRepresentation = [asset defaultRepresentation];
                 self.assetSize = [self.assetRepresentation size];
                 // Even though we don't really have any data, this will enable the response headers to be sent
                 // It will call our delayResponseHeaders method, which will now return NO, since we've set self.repr
                 [self.connection responseHasAvailableData:self];
             }
            failureBlock:^(NSError *error) {
                // recover from error, then
                NSLog(@"error in the failureBlock: %@",[error localizedDescription]);
            }];
    }
    return self;
    }
    
    - (NSData *)readDataOfLength:(NSUInteger)lengthParameter{
    HTTPLogTrace2(@"%@[%p]: readDataOfLength:%lu", THIS_FILE, self, (unsigned long)lengthParameter);
    
    NSUInteger remaining = self.assetSize - offset;
    NSUInteger length = lengthParameter < remaining ? lengthParameter : remaining;
    
    Byte *buffer = (Byte *)malloc(length);
    NSError *error;
    
    NSUInteger buffered = [self.assetRepresentation getBytes:buffer fromOffset:offset length:length error:&error];
    NSLog(@"Error: %@",[error localizedDescription]);
    NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
    
    // now that we've read data of length, update the offset for the next invocation
    offset += length;
    
    return data;
    }
    
    -(id)initWithAssetURL:(NSURL*)连接的assetURL:(MyHTTPConnection*)父级{
    if((self=[super init]))
    {
    HTTPLogTrace();
    //现在需要这个,这样我们可以在整个课堂上使用它
    自我连接=父母;
    self.assetURL=assetURL;
    偏移量=0;
    //抓取一个指向我们可以持久使用的库的指针
    self.lib=[[AlassetLibrary alloc]init];
    //确实需要知道资产的大小,但我们还将设置一个属性,以便稍后使用
    //否则,我们将不得不通过assetForURL再次异步查找资产
    [self.lib assetForURL:assetURL
    结果块:^(ALAsset*资产){
    self.assetRepresentation=[asset defaultRepresentation];
    self.assetSize=[self.assetresentation size];
    //即使我们实际上没有任何数据,这也将允许发送响应头
    //它将调用我们的delayResponseHeaders方法,该方法现在将返回NO,因为我们已经设置了self.repr
    [self.connection responseHasAvailableData:self];
    }
    故障块:^(N错误*错误){
    //然后从错误中恢复过来
    NSLog(@“failureBlock中的错误:%@,[error localizedDescription]);
    }];
    }
    回归自我;
    }
    -(NSData*)readDataOfLength:(NSInteger)Length参数{
    HTTPLogTrace2(@“%@[%p]:readDataOfLength:%lu”,此文件,self,(无符号长)长度参数);
    剩余整数=self.assetSize-偏移量;
    NSU整数长度=长度参数<剩余?长度参数:剩余;
    字节*缓冲区=(字节*)malloc(长度);
    n错误*错误;
    NSInteger buffered=[self.assetRepresentation getBytes:buffer fromOffset:offset length:length error:&error];
    NSLog(@“错误:%@,[Error localizedDescription]);
    NSData*data=[NSData data WITHBYTESNOPY:缓冲区长度:缓冲区空闲时间当域:是];
    //现在我们已经读取了长度数据,为下一次调用更新偏移量
    偏移量+=长度;
    返回数据;
    }