How to create Data from CMSampleBuffer With Swift 4

Multi tool use
up vote
0
down vote
favorite
We can convert CMSampleBuffer to NSData with following function.
But we could not find a way to convert it to Data.
We tried using
Data(bytes: <#T##UnsafeRawPointer#>, count: <#T##Int#>)
instead of
NSData(bytes: <#T##UnsafeRawPointer?#>, length: <#T##Int#>)
but no luck.
Is there anyone who could do it.
func frameData() -> NSData {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
return nsdata
}
ios swift4
add a comment |
up vote
0
down vote
favorite
We can convert CMSampleBuffer to NSData with following function.
But we could not find a way to convert it to Data.
We tried using
Data(bytes: <#T##UnsafeRawPointer#>, count: <#T##Int#>)
instead of
NSData(bytes: <#T##UnsafeRawPointer?#>, length: <#T##Int#>)
but no luck.
Is there anyone who could do it.
func frameData() -> NSData {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
return nsdata
}
ios swift4
Have you tried to convert NSData to Data as like this stackoverflow.com/a/49074149/3420996?
– Natarajan
Nov 7 at 8:20
Just seconds ago I tried this let data = Data(bytes: src_buff!, count: bytesPerRow * height) and seems to be working. (!)
– Hope
Nov 7 at 8:22
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
We can convert CMSampleBuffer to NSData with following function.
But we could not find a way to convert it to Data.
We tried using
Data(bytes: <#T##UnsafeRawPointer#>, count: <#T##Int#>)
instead of
NSData(bytes: <#T##UnsafeRawPointer?#>, length: <#T##Int#>)
but no luck.
Is there anyone who could do it.
func frameData() -> NSData {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
return nsdata
}
ios swift4
We can convert CMSampleBuffer to NSData with following function.
But we could not find a way to convert it to Data.
We tried using
Data(bytes: <#T##UnsafeRawPointer#>, count: <#T##Int#>)
instead of
NSData(bytes: <#T##UnsafeRawPointer?#>, length: <#T##Int#>)
but no luck.
Is there anyone who could do it.
func frameData() -> NSData {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
return nsdata
}
ios swift4
ios swift4
edited Nov 7 at 8:17
asked Nov 7 at 7:47
Hope
5432828
5432828
Have you tried to convert NSData to Data as like this stackoverflow.com/a/49074149/3420996?
– Natarajan
Nov 7 at 8:20
Just seconds ago I tried this let data = Data(bytes: src_buff!, count: bytesPerRow * height) and seems to be working. (!)
– Hope
Nov 7 at 8:22
add a comment |
Have you tried to convert NSData to Data as like this stackoverflow.com/a/49074149/3420996?
– Natarajan
Nov 7 at 8:20
Just seconds ago I tried this let data = Data(bytes: src_buff!, count: bytesPerRow * height) and seems to be working. (!)
– Hope
Nov 7 at 8:22
Have you tried to convert NSData to Data as like this stackoverflow.com/a/49074149/3420996?
– Natarajan
Nov 7 at 8:20
Have you tried to convert NSData to Data as like this stackoverflow.com/a/49074149/3420996?
– Natarajan
Nov 7 at 8:20
Just seconds ago I tried this let data = Data(bytes: src_buff!, count: bytesPerRow * height) and seems to be working. (!)
– Hope
Nov 7 at 8:22
Just seconds ago I tried this let data = Data(bytes: src_buff!, count: bytesPerRow * height) and seems to be working. (!)
– Hope
Nov 7 at 8:22
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Just used
let data = Data(bytes: src_buff!, count: bytesPerRow * height)
instead of
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
The key is here was ! after src_buff for Data.
Because xCode was showing some errors which is not related ! usage I could not understand ! was needed.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Just used
let data = Data(bytes: src_buff!, count: bytesPerRow * height)
instead of
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
The key is here was ! after src_buff for Data.
Because xCode was showing some errors which is not related ! usage I could not understand ! was needed.
add a comment |
up vote
0
down vote
accepted
Just used
let data = Data(bytes: src_buff!, count: bytesPerRow * height)
instead of
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
The key is here was ! after src_buff for Data.
Because xCode was showing some errors which is not related ! usage I could not understand ! was needed.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Just used
let data = Data(bytes: src_buff!, count: bytesPerRow * height)
instead of
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
The key is here was ! after src_buff for Data.
Because xCode was showing some errors which is not related ! usage I could not understand ! was needed.
Just used
let data = Data(bytes: src_buff!, count: bytesPerRow * height)
instead of
let nsdata = NSData(bytes: src_buff, length: bytesPerRow * height)
The key is here was ! after src_buff for Data.
Because xCode was showing some errors which is not related ! usage I could not understand ! was needed.
edited Nov 7 at 9:23
answered Nov 7 at 9:10
Hope
5432828
5432828
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185322%2fhow-to-create-data-from-cmsamplebuffer-with-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
V3tqqVrGFr,7OcqKSi0tDQ7I5M9MWuQi,E,RwD
Have you tried to convert NSData to Data as like this stackoverflow.com/a/49074149/3420996?
– Natarajan
Nov 7 at 8:20
Just seconds ago I tried this let data = Data(bytes: src_buff!, count: bytesPerRow * height) and seems to be working. (!)
– Hope
Nov 7 at 8:22