How to create Data from CMSampleBuffer With Swift 4











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

}









share|improve this question
























  • 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















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

}









share|improve this question
























  • 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













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

}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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
































    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 7 at 9:23

























        answered Nov 7 at 9:10









        Hope

        5432828




        5432828






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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




















































































            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()