FFmpeg-save streaming data as image in IOS

Source: Internet
Author: User
In the near future, we need to add the video capture function to the iPhone's streaming application to sort out our actual work experience.

Step 1. Use FFMPEG to obtain the image information in the stream and perform decoding.

av_read_frame(pFormatCtx,
&packet);avcodec_decode_video2(videoCodecCtx, DecodedFrame, &frameFinished, & packet);

Step 2. convert an image into a custom IOS uiimage

First, convert avframe In YUV format to avpicture in RGB format.

img_convert_ctx
= sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, outputWidth, outputHeight, PIX_FMT_RGB24, sws_flags, NULL, NULL, NULL);

Then, convert avpicture into IOS's uiimage.

-(UIImage
*)imageFromAVPicture:(AVPicture)pict width:(int)width height:(int)height { CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, pict.data[0], pict.linesize[0]*height,kCFAllocatorNull); CGDataProviderRef
provider = CGDataProviderCreateWithCFData(data); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGImageRef cgImage = CGImageCreate(width, height, 8, 24, pict.linesize[0], colorSpace, bitmapInfo, provider, NULL, NO, kCGRenderingIntentDefault);
CGColorSpaceRelease(colorSpace); UIImage *image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); CGDataProviderRelease(provider); CFRelease(data); return image;}

Step 3. Store the uiimage

Three methods

1. Save the statement to the specified object (PNG or JPEG)

[UIImageJPEGRepresentation(image,
1.0) writeToFile:jpgPath atomically:YES];[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES]; 

2. directly store the image into the album (JPEG)

void
UIImageWriteToSavedPhotosAlbum ( UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);

3. Save the case to a specific group in the album

IOS devices do not provide this function.
Therefore, you can take a look at the www.touch-code-magazine.com article and use the following customized API to do this.

-(void)saveImage:(UIImage*)image
toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;

Additional information:

  • Http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
  • Http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html
  • Http://albert-oma.blogspot.com/2013/05/ffmpeg-save-streaming-data-as-image-in.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.