I have two ImageViews and I am merging two images. First image is a bodyImage and second Image is a tattooImage. I already done merging, but I wants ask
1)I can drag tattooImage over bodyImage. I wants on doubleTap tattooImage mergeWith bodyImage on the tap coordinates. Hope you understand question
Thanks
+ =
and here is my code : here imageView1 is my bodyImage and imageView2 is my tattooImage
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
int width=500;
int height=500;
NSLog(#"takephoto from twitter");
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[imageView1.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[imageView2.image drawInRect:CGRectMake(180,200,200,200) blendMode:kCGBlendModeDarken alpha:0.4];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
imageView1.image=newImage;
UIGraphicsEndImageContext();
}
mask image http://www.developers-life.com/resize-and-mask-an-image.html
You need image masking for that. I wrote a tutorial on how to use it, and how I've used it in my own application. From the Apple documentation:
Masking techniques can produce many interesting effects by controlling which parts of an image are painted. You can:
Apply an image mask to an image. You can also use an image as a mask
to achieve an effect that’s opposite from applying an image mask.
Use color to mask parts of an image, which includes the technique
referred to as chroma key masking.
Clip a graphics context to an
image or image mask, which effectively masks an image (or any kind of
drawing) when Quartz draws the content to the clipped context.
Related
I have some problems in rescaling the content of a UILabel object when it is stored as an image. Since the rendered image has to be bigger than the original UILabel, I have computed the scale imageScale needed to rescale the original image and saved it into a CGSize variable. In the following, I will explain the adopted (and failing) approaches.
Code used for rendering the image
The following code is used for rendering the extracted image on the canvas.
[labelImage drawInRect:CGRectMake(xCoordinate/imageScale.width,
yCoordinate/imageScale.height,
newSize.width,
newSize.height)
blendMode:kCGBlendModeNormal alpha:0.8];
where the variable newSize is computed as follows:
newSize.width = originalWidth/imageScale.width;
newSize.height = originalHeight/imageScale.height
Approach 1
I extracted the label using the following code:
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[[label layer] renderInContext: UIGraphicsGetCurrentContext()];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
where label is the UILabel variable and newSize is the size that the rescaled image should have (see above for details).
However, I obtain the following image, which is obviously failing, since the content is very little and not centered:
Approach 2
I extracted the label using the following code:
UIGraphicsBeginImageContextWithOptions([label bounds].size, NO, 0.0);
[[label layer] renderInContext: UIGraphicsGetCurrentContext()];
UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
However, since I am using the original image size in order to extract the image, the effect I obtain is the following:
As you can notice, the text in the balloon has not a high resolution, and thus it is not visualized properly.
The question
How to correct one of the two approaches so as to visualize in high resolution the image?
Seems like you just need to set appropriate scale for the generated image.
This is the function:
void UIGraphicsBeginImageContextWithOptions(
CGSize size,
BOOL opaque,
CGFloat scale
);
You set scale as 0.0. Try replacing it with [UIScreen mainScreen].scale.
I've been having issues rendering images with the UIImageView class. The pixelation seems to occur mostly on the edges of the image I am trying to show.
I have tried changing the property 'Render with edge antialiasing' to no avail.
The image files contain images that are larger than what will appear on the screen.
It seems to be royally messing with the quality of the image and then displaying it. I tried to post images here, but StackOverflow is denying me that privilege. So here's a link to what's going on.
http://i.imgur.com/QpUOTOF.png
The sun in this image is the problem I'm speaking of. Any ideas?
On-the-fly image resizing is quick and of low quality. For bundled images, it is worth the extra bundle space to include downsized versions. For downloaded images, you can achieve better results by resizing with Core Graphics into a new UIImage before you set the image property.
CGSize newSize = CGSizeMake(newWidth, newHeight);
UIGraphicsBeginImageContextWithOptions(newSize, // context size
NO, // opaque?
0); // image scale. 0 means "device screen scale"
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
[bigImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Use following method use for get specific hight and width with image
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
CGSize newSize = CGSizeMake(width, height);
float widthRatio = newSize.width/image.size.width;
float heightRatio = newSize.height/image.size.height;
if(widthRatio > heightRatio)
{
newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
}
else
{
newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
}
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
This method return NewImage, with specific size that you specified.
How big is your image and what is the size of the imageView? Don't rely on UIImageView to scale it down for you. You probably need to resize it manually. This would also be a bit more memory efficient.
I use categories like these:
>>>github link <<<
to do image resizing.
This also gives you some other nice function for rounded corners etc.
Also keep in mind, that you need a transparent border at the edge of an image if you want to rotate it to avoid aliasing.
UIGraphicsBeginImageContext(backgroundImageView.frame.size);
UIImageView *image1 = backgroundImageView;
UIImageView *image2 = boat;
// Draw image1
[image1.image drawInRect:CGRectMake(0, 0, image1.image.size.width, image1.image.size.height)];
// Draw image2
[image2.image drawInRect:CGRectMake(boat.center.x, boat.center.y, boat.image.size.width, boat.image.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
When resultingImage is displayed the transform angle value for image2 is not preserved after I set it equal to the boat UIImageView. I thought that when I set image2 = boat that the transform value of boat would carry over to image2.
I suspect it relates to the fact that I am I am working with UIImageView instead of UIVIew. I have tried many different things but have been unable to resolve this issue.
Any help would be appreciated.
Thanks.
When you draw an image into a graphics context, it uses the context's transformation matrix, which is distinct from any transformation applied to a view. So the transformation on image2 (a UIImageView presumably) is completely irrelevant to direct image drawing.
You can either specify a new transformation with the various CGContext and CGAffineTransform functions in Quartz, or you can directly apply a copy of the UIImageView 's transformation like so:
CGContextConcatCTM(UIGraphicsGetCurrentContext(), someView.transform);
[someView.image drawInRect: CGRectMake(...)];
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...
I've got a UIImageView (full frame and rectangular) that i'm rotating with a CGAffineTransform. The UIImage of the UIImageView fills the entire frame. When the image is rotated and drawn the edges appear noticeably jagged. Is there anything I can do to make it look better? It's clearly not being anti-aliased with the background.
The edges of CoreAnimation layers aren't antialiased by default on iOS. However, there is a key that you can set in Info.plist that enables antialiasing of the edges: UIViewEdgeAntialiasing.
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html
If you don't want the performance overhead of enabling this option, a work-around is to add a 1px transparent border around the edge of the image. This means that the 'edges' of the image are no longer on the edge, so don't need special treatment!
New API – iOS 6/7
Also works for iOS 6, as noted by #Chris, but wasn't made public until iOS 7.
Since iOS 7, CALayer has a new property allowsEdgeAntialiasing which does exactly what you want in this case, without incurring the overhead of enabling it for all views in your application! This is a property of CALayer, so to enable this for a UIView you use myView.layer.allowsEdgeAntialiasing = YES.
just add 1px transparent border to your image
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
[image drawInRect:CGRectMake(1,1,image.size.width-2,image.size.height-2)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Remember to set the appropriate anti-alias options:
CGContextSetAllowsAntialiasing(theContext, true);
CGContextSetShouldAntialias(theContext, true);
just add "Renders with edge antialiasing" with YES in plist and it will work.
I would totally recommend the following library.
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
It contains lots of useful extensions to UIImage that solve this problem and also include code for generating thumbnails etc.
Enjoy!
The best way I've found to have smooth edges and a sharp image is to do this:
CGRect imageRect = CGRectMake(0, 0, self.photo.image.size.width, self.photo.image.size.height);
UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0.0);
[self.photo.image drawInRect:CGRectMake(1, 1, self.photo.image.size.width - 2, self.photo.image.size.height - 2)];
self.photo.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Adding the Info.plist key like some people describe has a big hit on performance and if you use that then you're basically applying it to everything instead of just the one place you need it.
Also, don't just use UIGraphicsBeginImageContext(imageRect.size); otherwise the layer will be blurry. You have to use UIGraphicsBeginImageContextWithOptions like I've shown.
I found this solution from here, and it's perfect:
+ (UIImage *)renderImageFromView:(UIView *)view withRect:(CGRect)frame transparentInsets:(UIEdgeInsets)insets {
CGSize imageSizeWithBorder = CGSizeMake(frame.size.width + insets.left + insets.right, frame.size.height + insets.top + insets.bottom);
// Create a new context of the desired size to render the image
UIGraphicsBeginImageContextWithOptions(imageSizeWithBorder, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Clip the context to the portion of the view we will draw
CGContextClipToRect(context, (CGRect){{insets.left, insets.top}, frame.size});
// Translate it, to the desired position
CGContextTranslateCTM(context, -frame.origin.x + insets.left, -frame.origin.y + insets.top);
// Render the view as image
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
// Fetch the image
UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
// Cleanup
UIGraphicsEndImageContext();
return renderedImage;
}
usage:
UIImage *image = [UIImage renderImageFromView:view withRect:view.bounds transparentInsets:UIEdgeInsetsZero];