你好,提前感谢你抽出时间来研究我的问题。
我正在创建一个位图,然后从该位图构建一个PNG。然后我把PNG保存到相册中。在通过其本地标识符取回该资产,然后使用PHImageManager检索与该资产相关联的UIImage之后,我注意到该图像的字节已更改。我不确定在保存为相位集时是否添加了头,但我没有看到插入和检索图像之间的任何公共字节。不过,当我把检索到的图像放到视图中时,它看起来足够正确,所以我很困惑。
下面是一些演示该问题的示例代码。

// image is a UIImage

// This is to show what the bytes are prior to saving
let imageRef = image.CGImage!
let cgData = CGDataProviderCopyData(CGImageGetDataProvider(imageRef))
let data = NSData(data: cgData)
let bytes = UnsafeMutablePointer<UInt8>(data.bytes)

print(data.length)
for i in 0..<16 {
    print(bytes[i])
}

var assetIdentifier: String!

// collectionIdentifier is a valid local identifier for an existing collection
let collectionFetchRequest = PHAssetCollection.fetAssetCollectionsWithLocalIdentifiers([collectionIdentifier], options: nil)
let collection = collectionFetchRequest.firstObject as! PHAssetCollection

PHPhotoLibrary.sharedPhotoLibrary().performChanges({

    let assetCreationRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
    let collectionChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: collection)!
    let assetPlaceholder = assetCreationRequest.placeholderForCreatedAsset!
    collectionChangeRequest.addAssets([assetPlaceholder])
    assetIdentifier = assetPlaceholder.localIdentifier

}) { (_, _) in

    let assetFetchRequest = PHAsset.fetchAssetsWithLocalIdentifiers([assetIdentifier], options: nil)
    let asset = assetFetchRequest.firstObject as! PHAsset

    let options = PHImageRequestOptions()
    options.synchronous = true
    options.version = .Current
    options.resizeMode = .None

    // The image is actually 64 x 64, but the targetSize doesn't seem to affect the bytes I'm getting out, I assume because the options aren't saying not to resize or crop anything
    // This was the not working code.
    // PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: CGSizeMake(256, 256), contentMode: .Default, options: options) { (retrievedImage, _) in

    // This is the working code.
    PHImageManager.defaultManager().requestImageDataForAsset(asset, options: options) { (data, _, _, _) in

        let retrievedImage = UIImage(data: data!)!

        // This is to show what the bytes are after retrieving
        let retrievedCGImage = retrievedImage!.CGImage!
        let retrievedCFData = CGDataProviderCopyData(CGImageGetDataProvider(retrievedCGImage))!
        let retrievedData = NSData(data: retrievedCFData)
        let retrievedBytes = UnsafeMutablePointer<UInt8>(retrievedData.bytes)

        // Length is the same but the bytes are not even close to right
        print(retrievedData.length)
        for i in 0..<16 {
            print(retrievedBytes[i])
        }

    }
}

请随意更正此代码中任何不正确或效率低下的部分。这段代码应该运行,所以如果您看到任何语法错误或类型错误的变量,它们只在StackOverflow上,而不是在XCode中,所以它们不是这个字节问题的原因。同样,对于图像中的字节,我不关心第四个字节,这是alpha组件,因为从目前的测试来看,它始终是255。如果有人知道我如何使用第4个字节,而不需要在保存图像时将其设置为255,或者只是没有alpha组件,那也会很有帮助。
再次感谢您的时间和考虑!
编辑:
以下是对回答者的完整回复:
谢谢你的提醒。事实证明,您对requestImageDataForAsset工作的看法是正确的。我将编辑原始代码块以反映工作代码。您知道文档中它在哪里描述了requestImageForAsset返回的图像吗?
此外,您知道任何描述由requestImageDataForAsset返回的数据的文档吗?我不太熟悉图片存储机制,但是从requestImageData返回的数据在转换成UIImage之前也与图片不完全匹配。
再次感谢!
StackOverflow可爱的界面让一个enter键提交评论,然后给最多5分钟的编辑时间。那太好了。

最佳答案

requestImageForAsset方法不能保证获得字节完美输出。我建议您尝试requestImageDataForAsset方法,它将给出您期望的结果。

关于ios - 来自PHImageManager的图像数据与另存为 Assets 的图像数据不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35436842/

10-14 20:30
查看更多