I need to send images read from the Photo Library over the wire - in chunks of 5MB.
I read an image from the library using: PHImageManager.requestImageData(for:options:resultHandler:)
and get a Data
object. I would then like to efficiently split the data into chunks (without copying the memory). What would be the best way to do that?
This is what I have so far:
imageData.withUnsafeBytes { (unsafePointer: UnsafePointer<UInt8>) -> Void in
let totalSize = data.endIndex
var offset = 0
while offset < totalSize {
let chunkSize = offset + uploadChunkSize > totalSize ? totalSize - offset : uploadChunkSize
let chunk = Data(bytesNoCopy: unsafePointer, count: chunkSize, deallocator: Data.Deallocator.none)
// send the chunk...
offset += chunkSize
}
}
However I get this error at compile time:
Cannot convert value of type 'UnsafePointer' to expected argument type 'UnsafeMutableRawPointer'
If I use mutableBytes:
data.withUnsafeMutableBytes { (unsafePointer: UnsafeMutablePointer<UInt8>) -> Void in... }
Then I get the compile-time error:
Cannot use mutating member on immutable value: 'data' is a 'let' constant
Which is correct, since I do not really want to make changes to the image data. I only want to send one chunk of it at a time.
Is there a better way to do this?