CFReadStreamOpen 返回错误 kCFStreamErrorDomainPOSIXDomain

问题描述 投票:0回答:1

我想制作图像的 MD5,所以我使用下面的代码来完成此操作

CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath, size_t chunkSizeForReadingData, BOOL *isAbort)
{
    // Declare needed variables
    CFStringRef result = NULL;
    CFReadStreamRef readStream = NULL;

    // Get the file URL
    CFURLRef fileURL =
    CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                                  (CFStringRef)filePath,
                                  kCFURLPOSIXPathStyle,
                                  (Boolean)false);
    if (!fileURL) goto done;

    // Create and open the read stream
    readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,
                                            (CFURLRef)fileURL);
    if (!readStream) goto done;
    bool didSucceed = (bool)CFReadStreamOpen(readStream);
    if (!didSucceed) goto done;

    // Initialize the hash object
    CC_MD5_CTX hashObject;
    CC_MD5_Init(&hashObject);

    // Make sure chunkSizeForReadingData is valid
    if (!chunkSizeForReadingData) {
        chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;
    }

    // Feed the data to the hash object
    bool hasMoreData = true;
    while (hasMoreData) {

        if(*isAbort){
            NSLog(@"hasMoreData isAborted");
            if (readStream) {
                CFReadStreamClose(readStream);
                CFRelease(readStream);
            }
            if (fileURL) {
                CFRelease(fileURL);
            }
            return nil;
        }

        uint8_t buffer[chunkSizeForReadingData];
        CFIndex readBytesCount = CFReadStreamRead(readStream,
                                                  (UInt8 *)buffer,
                                                  (CFIndex)sizeof(buffer));
        if (readBytesCount == -1) break;
        if (readBytesCount == 0) {
            hasMoreData = false;
            continue;
        }
        CC_MD5_Update(&hashObject,
                      (const void *)buffer,
                      (CC_LONG)readBytesCount);
    }

    // Check if the read operation succeeded
    didSucceed = !hasMoreData;

    // Compute the hash digest
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5_Final(digest, &hashObject);

    // Abort if the read operation failed
    if (!didSucceed) goto done;

    // Compute the string result
    char hash[2 * sizeof(digest) + 1];
    for (size_t i = 0; i < sizeof(digest); ++i) {
        snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i]));
    }
    result = CFStringCreateWithCString(kCFAllocatorDefault,
                                       (const char *)hash,
                                       kCFStringEncodingUTF8);
done:

    if (readStream) {
        CFReadStreamClose(readStream);
        CFRelease(readStream);
    }
    if (fileURL) {
        CFRelease(fileURL);
    }
    return result;
}

上面的代码在 Mac OS 中可以正常工作,但是我在 iOS 中做了同样的事情,但它不起作用。据我所知,我传递的 fileUrl 完全正确,但在 CFReadStreamOpen() 中它返回 kCFStreamErrorDomainPOSIX 并且我开始知道这个错误号是 1 并且它指出它是“操作不允许”

我已浏览此链接

如何读取 iOS 设备的任何 PHAsset 流

更改以下文件所有权是否会影响此上下文:

ios objective-c cocoa-touch cfreadstream
1个回答
0
投票

PHAsset 仅包含有关图像的元数据。为了获取图像数据,您需要使用 PHImageManager。

func requestImageData(for asset: PHAsset, 
              options: PHImageRequestOptions?, 
        resultHandler: @escaping (Data?, String?, UIImageOrientation, [AnyHashable : Any]?) -> Void) -> PHImageRequestID

您可以使用 CFReadStreamCreateWithBytesNoCopy 创建带有数据的 CFReadStreamRef。

© www.soinside.com 2019 - 2024. All rights reserved.