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