Is it possible to merge two UIImageViews with respect of content mode ? I have two UIImageViews one is AspectFit and One is Aspect Fill when i blend both images it combines correctly but not with the exact content mode.
Here is the code
CGSize newSize = CGSizeMake(fx_imageView.image.size.width,fx_imageView.image.size.height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[fx_imageView.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[broderImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// resultImage.image = result;
Related
This is my Transparent image (Front image).
This is my selected image (Back Image).
I am giving gestures to the back image.
How can i save my image after using of pan, pinch, rotate gestures.
Present am able to combine both images but back image is saving as it is like (below image)
Using this code
CGSize newSize = CGSizeMake(640, 960);
UIGraphicsBeginImageContext( newSize);
[backImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[frontImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(finalImage, nil, nil, nil);
I think i should handle this two lines but i don't know how to handle.
[backImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[frontImage.image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
Can any one help me will highly appreciated.
Thank you.
Please find the below code to get snapshot your imageview.
UIGraphicsBeginImageContextWithOptions(yourImageViewToSnapShot.bounds.size, NO, 0.0);
[yourImageViewToSnapShot.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I am not sure whether I can improve image quality but following code displays very poor image quality in PDF. I know its standard code to generate images from view but is there anything I could do to specify image quality or improve it?
- (void)renderView:(UIView*)view {
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewAsImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[viewAsImage drawInRect:rect];
}
You probably need to create a graphics context with a scale of 2 (retina) instead of the default 1. To do so, use UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0.0);. This will create an image context with an opaque target (you can set the second parameter to NO if you're rendering a transparent image) and with a scale factor of your device's main screen.
Double the size!
- (void)renderView:(UIView*)view {
CGSize newSize = view.frame.size;
newSize.width = newSize.width * [UIScreen mainScreen].nativeScale;
newSize.height = newSize.height * [UIScreen mainScreen].nativeScale;
UIGraphicsBeginImageContext(newSize);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewAsImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[viewAsImage drawInRect:rect];
}
I have a UIImageView with a size of (144,130) and the image size I am getting after capturing is (720,960).
I have resized image with
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Here new size is (144,130) which I have given because it is the imageviews size. Even though the image is getting stretched.
What should I make changes so that image does not get stretched?
EDIT-1
UIImage *imageCapture = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImage *finalImage = [self imageByScalingAndCroppingForSize:(imageview.frame.size)];
as per the answer in this link
EDIT-2
Image captured has size : {720, 960}
imageview.frame.size : {144, 130}
newSize : {97.5, 130}
with resizing u have to maintain the aspect ratio. just match your view's aspect ratio with the actual image and there won't be any stretched image...
this could be useful for you
You have set the Imageview Property
imageView.contentMode=UIViewContentModeScaleAspectFit;
and if you are using nib then set the imageview property in View section-> Mode -> Aspect Fit
There is no need to resize your image. It worked for me.
And though you want to resize the image you can have following image in UIImage category
- (UIImage *) scaleProportionalToSize: (CGSize)size
{
float widthRatio = size.width/self.size.width;
float heightRatio = size.height/self.size.height;
if(widthRatio > heightRatio)
{
size=CGSizeMake(self.size.width*heightRatio,self.size.height*heightRatio);
} else {
size=CGSizeMake(self.size.width*widthRatio,self.size.height*widthRatio);
}
return [self scaledToSize:size];
}
- (UIImage *)scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[newImage autorelease];
return newImage;
}
When you are resizing image to newSize you must take into account that, if you are resize width by suppose 10% then you must resize height by 10% only.
I tried to resize image to fit the frame with the code below but the image is kind of distorted.
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
UIImage* image =[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
deal.Image=[tfbImageHelper imageWithImage:image scaledToSize:newSize];
Yes, because you should use UIGraphicsBeginImageContextWithOptions and the last argument should be 0.0f (which means, to keep the current scale). It's distorted, because you're testing it on a retina display and the scale factor there is 2. Also make sure, you are keeping the aspect ratio, when assigning the new size.
i am working on app which allow to capture image from cam or select from photo library, after that it allow to select some options like what border color to apply, border width, rounded or square corners, what size user want like 45*45mm or 70*70mm etc and text user wants to apply at the bottom of this whole image and then app will save it as a whole image.
For border colour, border width, border corner i have create images in photoshop of different colour.
I am stuck at that how should i approach or how should i apply border properties and image captured and text to create a whole new image and save it. And how to apply 45*45mm or 70*70mm different sizes to the image.
Example code for adding one image to another one
- (void)drawRect:(CGRect)rect {
UIImage *bottomImage = [[UIImage imageNamed:#"background.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:0];
UIImage *image = [UIImage imageNamed:#"logo.png"];
CGSize newSize = CGSizeMake(rect.size.width, rect.size.height);
UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[image drawAtPoint:CGPointMake((int)((newSize.width - image.size.width) / 2), (int)((newSize.height - image.size.height) / 2))];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[newImage drawInRect:rect];
}
logo always lies on center
For resizing and rounding images i use
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/