save an image with two UIImageView which has two images - ios [duplicate] - iphone

This question already has answers here:
How can i add two uiimageview and save it to iphone photo gallery in iphone application development?
(2 answers)
Closed 9 years ago.
In my application I have two UIImageViews which is overlayed one over the other. i.e ) UIImageView-2 is placed on UIImageView-1. The image in UIImageView-2 is transparent, so now I have image over another image. Now I need to save these two as a single image. Is it possible in ios? Please advice..
The sample view is as below: The red mark is an image in a view over tennis ball image view.

Hope following method will help you.
-(CGImageRef )mergedImageFromImageOne:(UIImage *)imageOne andImageTwo:(UIImage *)imageTwo
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGSize imageSize = imageOne.size;
UIGraphicsBeginImageContext(sizeVideo);
[imageOne drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
[imageTwo drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height) alpha:1];
CGImageRef imageRefNew = CGImageCreateWithImageInRect(UIGraphicsGetImageFromCurrentImageContext().CGImage, CGRectMake(0,0,imageOne.width,imageOne.height));
UIGraphicsEndImageContext();
[pool release];
return imageRefNew;
}

I've taken one Logo Image which is set on main image as application ref.
So you can refer this, try with your images.
set width(w) & height(h) as per your image.
Try this ::
UIImage *img_Logo = [UIImage imageNamed:#"Img_Logo.png"];
CGSize newSize = CGSizeMake(w, h);
UIGraphicsBeginImageContext(newSize);
// Use existing opacity as is
[main_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[img_Logo drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
main_image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgView.image = main_image;
Hope, this'll help you.
Thanks.

Why you are not taking screenshot for only that portion??

Related

iphone: Scale the image in table cell

I am using a UITableViewCellStyleSubTitle. I am trying to add thumbnails to my table cells.
UIViewContentModeScaleAspectFit does not seem to work for reasons unknown. I have seen a lot of answers in stack overflow for similar kind of questions. If i crop the image using drawInRect it works, like this
[thumbnail drawInRect:thumbnailRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
But what i am trying to achieve should not need me to do this. I am only trying to scale the image and not crop it.
So setting the content mode to "UIViewContentModeScaleAspectFit" for the imageview of the default cell should work.. but it does not.
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
Complete logic for setting thumbnails.. fPath is the path to image
UIImage *thumbnail = [UIImage imageWithContentsOfFile:fPath];
[cell.imageView setImage:thumbnail];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.clipsToBounds = YES;
I found out that drawInRect stretches the original image if the size is lower or croppes it if it is bigger.
This article will help you to prepare a thumbnail of your image
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
It has several useful UIImage classes extentions:
* UIImage+Resize
* UIImage+Alpha
* UIImage+RoundedCorner
Example Usage:
UIImage *imageThumb = [YourImage thumbnailImage:70 transparentBorder:0 cornerRadius:10 interpolationQuality:kCGInterpolationHigh];
and then you can use drawInRect method. If the image is not rectangle - the image will crop the bigger side and take the middle of the image
Hope this answer will guide you.

Resize UIImage for editing and save at a high resolution?

Is there a way to load a picture from the library or take a new one, resize it to a smaller size to be able to edit it and then save it at the original size? I'm struggling with this and can't make it to work. I have the resize code setup like this :
firstImage = [firstImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(960, 640) interpolationQuality:kCGInterpolationHigh];
and then i have an UIImageView :
[finalImage setFrame:CGRectMake(10, 100, 300, 232)];
finalImage.image = firstImage;
if I setup the CGSizeMake at the original size of the picture it is a very slow process. I saw in other apps that they work on a smaller image and the editing process is fairly quick even for effects. What's the approach on this?
You can refer to Move and Scale Demo. This is a custom control which implements Moving and scaling and cropping image which can be really helpful to you.
Also this is the most simple code for scaling images to given size. Refer to it here: Resize/Scale of an Image
You can refer to it's code here
// UIImage+Scale.h
#interface UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)size;
#end
Implementation UIImage Scale Category
With the interface in place, let’s write the code for the method that will be added to the UIImage class.
// UIImage+Scale.h
#import "UIImage+Scale.h"
#implementation UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)size
{
// Create a bitmap graphics context
// This will also set it as the current context
UIGraphicsBeginImageContext(size);
// Draw the scaled image in the current context
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
// Create a new image from current context
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// Pop the current context from the stack
UIGraphicsEndImageContext();
// Return our new scaled image
return scaledImage;
}
#end
Using the UIImage Scale Method
Calling the new scale method we added to UIImage is as simple as this:
#import "UIImage+Scale.h"
...
// Create an image
UIImage *image = [UIImage imageNamed:#"myImage.png"];
// Scale the image
UIImage *scaledImage = [image scaleToSize:CGSizeMake(25.0f, 35.0f)];
Let me know if you want more help.
Hope this helps you.

Capturing 3d object with its Background view in iPhone

I am new in Open GLES.
I got stuck at capturing the 3d object with its Background image.
I am adding the CAEAGLLayer view as subview in my view
and i am able to take the image of 3d object but it is coming with black background but i want to take the image of whole view on which i am showing my 3d object.
So please help me to resolve this issue.
You need to give more information. What is the background?
You use 1 view, it is EAGLView the I guess you should get the correct result.
You use EAGLView as subview of your background then you need to capture 2 images then combine them into one.
Call [CALayer drawInContext:viewContext] to get image of your background view.
Combine 2 images
+ (UIImage *) imageFromImage:(UIImage *)img1 andImage:(UIImage*) img2 {
UIGraphicsBeginImageContext(img1.size);
[img1 drawAtPoint:CGPointMake(0, 0)];
[img2 drawAtPoint:CGPointMake(0, 0)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

reduce image size in image view in iphone application

I need to reduce image size in iphone.
I found sending programmatically email in google,But did n't found clear info regarding reduce size of image view.
Can any one pls post some sample code.
Thank u in advance.
you can do it like:
UIImage* newImage = nil;
UIGraphicsBeginImageContext(newFrame); // this will crop
[sourceImage drawInRect:newFrame];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This will draw the image in the frame you specified. Now if you want to maintain the aspect ratio also you can have look at Maintain the aspect ratio of an image?
As I saw that you are using image view, so don't use first part of code I mentioned. Now you have to do only:
[imageView setContentMode:UIViewContentModeScaleAspectFit];
Do you mean scaling a view that an image is contained within, or do you mean you want to resample image data to make an image with a smaller file size?
First couple of results in google bring up this... Resize Image
Instead take UIButton and set the image as its backgroundImage property... It will automatically resize the image to the size of the button.
-(UIImage*)createThumbNailImage:(UIImage*)myThumbNail:(float)width:(float)height
{
// begin an image context that will essentially "hold" our new image
UIGraphicsBeginImageContext(CGSizeMake(width,height));
// now redraw our image in a smaller rectangle.
[myThumbNail drawInRect:CGRectMake(0.0, 0.0, width, height)];
// make a "copy" of the image from the current context
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [newImage autorelease];
}
hAPPY iCODING...

Add Text to the Image

HI
Im currently developing an application where i have to add the text over the image at any position in the image(not subview) and the output should be the single image file with the original image and the text embedded in it,any help will be appreciable.
eg : water mark on the image
Thanks
sivasankar
UIImage *myImage = loadUnwatermarkedImage();
NSString *myWatermarkText = #"Watermark";
UIImage *watermarkedImage = nil;
UIGraphicsBeginImageContext(myImage.size);
[myImage drawAtPoint: CGPointZero];
[myWatermarkText drawAtPoint: CGPointMake(10, 10) withFont: [UIFont systemFontOfSize: 12]];
watermarkedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upon completion the watermarkedImage will be an autoreleased watermarked image. loadUnwatermarkedImage() is a fictional function providing an original image.