Creating Thumbnail from Video - Improving Speed Performance via MPMoviePlayerController - ios

This is my code by which is i am using. I want to generate thumbnail after that i create a video via these thumbnail with drawing which is done on "drawImage" UIImageView.
while([self.thunbnailArray count])
{
id times = [self.thunbnailArray objectAtIndex:0];////self.thumbnailArray contains duratin of player at which thumbnail generate//////
[self.thunbnailArray removeObjectAtIndex:0];
UIImage* thumbnail = [player1 thumbnailImageAtTime:[times floatValue] timeOption:MPMovieTimeOptionExact];
UIGraphicsBeginImageContext(drawImage.frame.size);////drawImage is a UIImageView to make drawing
[drawImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef firstImageRef = thumbnail.CGImage;/////merge thumbnail and drawing both together
CGFloat firstWidth = CGImageGetWidth(firstImageRef);
CGFloat firstHeight = CGImageGetHeight(firstImageRef);
CGSize mergedSize = CGSizeMake(firstWidth, firstHeight);
UIGraphicsBeginImageContext(mergedSize);
[thumbnail drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
[viewImage drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.imageArray addObject:newImage];///self.imageArray used to generate video
}

Related

Clipping large png make the ios app crash

- (UIImage *)createThumbnailImage:(UIImage *)image withSize:(CGSize)size {
CGRect imageRect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, CGRectMake(0, 0, size.width, size.height));
CGContextSetInterpolationQuality(context, 0.8);
[image drawInRect:imageRect blendMode:kCGBlendModeNormal alpha:1];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return thumbnail;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *inputImage = [UIImage imageNamed:#"dog.jpg"];
UIImage *image = [self createThumbnailImage:inputImage withSize:CGSizeMake(640.0, 480.0)]
}
I got a thumbnail image(640 * 480) by code above. And some odd problem confused me.
When I sent a jpg (10000 * 10000) to the method,it worked well.
But when I sent a png with the same size, the app would crash.
I tried to find some documents about the difference between jpg and png, but it made no sense.
Does anyone have any idea about this bug?
-(UIImage *)getNeedImageFrom:(UIImage*)image cropRect:(CGRect)rect
{
CGImageRef subImage = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *croppedImage = [UIImage imageWithCGImage:subImage];
CGImageRelease(subImage);
return croppedImage;
}
can you please check if this solve your problem

Download image through AFNetworking & edit it

I'm trying to download some image files from a remote server through the AFNetworking (UIImageView+AFNetworking.h) & retrieve the UIImage from it, edit that image. Editing means add that image(downloaded image) on top of another png file.( background image - UIImage )
I have tried several code blocks finally I'm stuck in here . I'm getting a black box only. Can't see the actual server image .
-(UIImage *)downloadImages:(NSString *)url{
UIImageView *downloadedImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,40,40)];
[downloadedImageView setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:#"Loading_image"]];
UIGraphicsBeginImageContextWithOptions(downloadedImageView.bounds.size, downloadedImageView.opaque, 0.0);
[downloadedImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Then I'm calling through that function in a for loop.
for( int i=0;i<[Images count];i++){
NSString *image_Url = [NSString stringWithFormat:#"%#%#",imageURL, imagename];
UIImage *downloadimage =[[UIImage alloc]init];
downloadimage = [self downloadImages:image_Url];
UIImage *bottomImage = [UIImage imageNamed:#"map_marker_black"];
CGSize newSize = CGSizeMake(60, 60);
UIGraphicsBeginImageContext( newSize );
[backgroundImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[downloadimage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.7];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Where am I doing wrong ? Your help is highly appreciated. Thanks a lot
A few issues. Lets start with the download code. There is no reason to deal with an image view and drawing it to create an image. Just use the actual image:
- (UIImage *)downloadImage:(NSString *)url{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
The big issue with your original code is that the AFNetworking method you used runs in the background. So you were always trying to draw the "loading" image.
Now the drawing code:
// Only need to load this once
UIImage *backgroundImage = [UIImage imageNamed:#"map_marker_black"];
// What is the loop used for?
for (NSUInteger i = 0; i < [Images count]; i++){
// Where does imageURL and imagename come from?
NSString *imageURL = [NSString stringWithFormat:#"%#%#",imageURL, imagename];
UIImage *downloadImage = [self downloadImage:imageURL];
CGRect newRect = CGRectMake(0, 0, 60, 60);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
[backgroundImage drawInRect:newRect];
[downloadimage drawInRect:newRect blendMode:kCGBlendModeNormal alpha:0.7];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// Do something here with newImage
UIGraphicsEndImageContext();
}
Note the comments in the code.

Move,rotate and scale UIImages, then merge them! in iphone

rotate image merge but not working in this code
i post my code
please give me solution
- (void)mergeImage :(UIImage *)imageA withImage:(UIImage *)imageB ###
{
UIImage *image0 = imageA;
UIImage *image1=overlayimg.image;
CGSize newImageSize = CGSizeMake(overlayimg.frame.size.width, overlayimg.frame.size.height);
UIGraphicsBeginImageContext(newImageSize);
[image0 drawInRect:CGRectMake(overlayimg.frame.origin.x,overlayimg.frame.origin.y,newImageSize.width,newImageSize.height)];
NSLog(#"Last Rotation:%f",templastrotate);
overlayimg.transform = CGAffineTransformMakeRotation(templastrotate);
CGContextConcatCTM(UIGraphicsGetCurrentContext(), overlayimg.transform);
[image1 drawInRect:CGRectMake(overlayView.frame.origin.x, overlayView.frame.origin.y,overlayView.frame.size.width,overlayView.frame.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
imageView.contentMode = UIViewContentModeScaleAspectFill & UIViewContentModeScaleAspectFit;
imageView.image=newImage;
UIGraphicsEndImageContext();
}

iOS capture UIView with UIImageView inside

I'm trying to get a UIView screen capture on iOS.
In my view I have two UIImageView with animations.
The code that I using it to capture the view is:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Read the UIImage object
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(image:didFinishSavingWithError:contextInfo:), nil);
The result is an image with the background and only one UIImageView. Both ImageViews are in moviming all the time. If I take several pictures, then the UIImageView is in the same position each time.
Try this one:
-(CGImageRef)imageCapture
{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect= CGRectMake(0,0 ,width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
return imageRef;
}
use the below line whenever you want to capture the screen
UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];

Combining images not working in device iOS

I am trying to combine two images into one. This is the code I am using:
UIImage* image1 = [UIImage imageNamed:#"1.tif"];
UIImage* image2 = [UIImage imageNamed:#"sign.tif"];
UIGraphicsBeginImageContext(image1.size);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView1.image = combinedImage;
It works perfectly in the simulator but does not work on the device. I get a white screen. Can any one please help me with this.
Any help will be appreciated.
Try to use this function:
- (UIImage * ) mergeImage: (UIImage *) imageA
withImage: (UIImage *) imageB
strength: (float) strength {
UIGraphicsBeginImageContextWithOptions(CGSizeMake([imageA size].width,[imageA size].height), NO, 0.0);
[imageA drawAtPoint: CGPointMake(0,0)];
[imageB drawAtPoint: CGPointMake(0,0)
blendMode: kCGBlendModeNormal // you can play with this
alpha: strength]; // 0 - 1
UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return mergedImage;
}
And call it like this:
UIImage* image1 = [UIImage imageNamed:#"1.tif"];
UIImage* image2 = [UIImage imageNamed:#"sign.tif"];
UIImage *mergedImage =[self mergeImage:image1 withImage:image2 strength:1];

Resources