Programmatically set the resolution of image in iphone? - iphone

Does anyone have the experience in dealing with image resolutions? Is it possible doing with Iphone/ipad?

-(UIImage*)processImageRect:(UIImage*)image:(CGSize)size:(CGSize)originalSize {
// Draw image1
UIGraphicsBeginImageContext(originalSize);
[image drawInRect:CGRectMake(0.0, 0.0, size.width, size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
return resultingImage;
}
//This function will be helpful to you.

Related

Building an Image From Multiple UIImageViews

I am interested in building a Collage style app for the IPhone and IPad. I have been looking at other apps and trying to work out the method that they use to create the final image.
I am guessing that the app has a UIView and then each individual image is a UIImageView and is a subview of the main UIView. The user is then able to move each UIImageView and position them as they intend.
Now my question is, after the user has positioned all the UIImageViews on screen how do you then create the final image from what is on the screen. This of course takes into account that you will be upscaling the images to create lets say an A4 sized final print.
Can anyone share some information?
Thanks
You should take a look at CGContext Reference. You can do something like the following to combine imageView 1 and 2 to imageView 3.
Sample Code :
UIGraphicsBeginImageContext(imageView1.image.size);
CGRect rect = CGRectMake(0, 0, imageView1.image.size.width, imageView1.image.size.height);
[imageView1.image drawInRect:rect];
[imageView2.image drawInRect:rect blendMode:kCGBlendModeScreen alpha:0.5];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imageView3 setImage:resultingImage];
GoodLuck !!!
If you use an UIView to include more 2 Images to merge, that you can try to capture the view to be an image to use.
//Capture View to Image
-(UIImage *)captureImageFromView
{
UIView *theView = self; //self extends UIView
if( NULL != UIGraphicsBeginImageContextWithOptions )
{
UIGraphicsBeginImageContextWithOptions(theView.bounds.size, NO, 2.0f);
}
else
{
//iOS 4.0
UIGraphicsBeginImageContext(theView.frame.size);
}
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return captureImage;
}
//Merge 2 Images to be an Image.
-(UIImage *)mergeBaseImage:(UIImage *)_baseImage
underImage:(UIImage *)_underImage
matchBaseImageSize:(BOOL)_matchBaseImageSize
{
UIGraphicsBeginImageContext(_baseImage.size);
[_baseImage drawInRect:CGRectMake(0, 0, _baseImage.size.width, _baseImage.size.height)];
if( _matchBaseImageSize )
{
[_underImage drawInRect:CGRectMake(0, 0, _baseImage.size.width, _baseImage.size.height)];
}
else
{
[_underImage drawInRect:CGRectMake(0, 0, _underImage.size.width, _underImage.size.height)];
}
UIImage *_mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return _mergedImage;
}
Wish it can help you.

Not able to merge two images into one

I'm trying to merge two images in one, and save that image onto the camera roll. But it just show a blank image. Can anyone help?
My code:
-(void)SaveFinalImage{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *savedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(savedImg, nil, nil, nil);
}
I have used this in my app.
UIImage *bottomImage = [UIImage imageNamed:#"bottom.png"]; //background image
UIImage *image = [UIImage imageNamed:#"top.png"]; //foreground image
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
for more see my related answer on the same subject. iOS - Merging two images of different size

convert rectangular image to square image using objective c

I am working on creating an image gallery which has thumbnails in different sizes. I want to convert these rectangle thumbnails to square size so that all of them could appear similar in size. I dont mind cropping it from extended portion but I am not sure how to do it. can anyone please help me?
Thanks
Pankaj
You need to use the image in rect method passing in the image and the required bounds...
CGImageRef imageRef = CGImageCreateWithImageInRect([anImage CGImage], requiredBounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
I have added this to a UIImage category (UIImage+Resize) in the following post, you can download the source code as well - Categories example
Well if you use an UIImageView to display your images (wich I am more than sure that you do) you can set it's contentMode property to UIViewContentModeScaleAspectFill. This should 'crop' your image to the boundaries of your UIImageView. In case your image will go out of the boundaries of the UIImageView make sure clipsToBounds is also set to YES.
Let me know if that helps.
I'm using the next method. The input are the UIImage to scale and the size of the UIImageView's frame where the UIImage is. It works when the frame's height and width are equal.
One important thing: I keep the image's ratio. I don't expand the image to cover the full square. If you want to do it you have to change the 'drawInRect' line for [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; and remove the if-else.
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
CGFloat scaleRatio;
if (image.size.width > image.size.height) {
scaleRatio = image.size.height/image.size.width;
}else{
scaleRatio = image.size.width/image.size.height;
}
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextConcatCTM(context, scaleTransform);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
if (image.size.width > image.size.height) {
[image drawInRect:CGRectMake(0, (newSize.height/2)-(newSize.height*scaleRatio/2), newSize.width, newSize.height*scaleRatio)];
}else{
[image drawInRect:CGRectMake((newSize.width/2)-(newSize.width*scaleRatio/2), 0, newSize.width*scaleRatio, newSize.height)];
}
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

How do i get it to scale my UIimage to a different size?

I have an image formed by a number of sublayers on a UIView, and then it takes a screenshot as so:
UIGraphicsBeginImageContext(object.bounds.size);
[object.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then it puts it onto a button:
[objectButton setImage:screenShot forState:UIControlStateNormal];
But the image displays at it's own size, so if the button isn't the same size as the image then i have a problem of it not making the image bigger or smaller.
How can i get it to change the size of the image to the size of the button?
Try this category -
.h file -
#interface UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size;
#end
.m file -
#implementation UIImage (UIImageAdditions)
- (UIImage*)scaleToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), self.CGImage);
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
#end
you can call it like this -
[imageObject scaleToSize:CGSizeMake(weight, height)];
Hope it help!
Try Like this,
[objectButton setBackgroundImage:screenShot forState:UIControlStateNormal];
You can change the contentMode on the UIButton to scale to fit. This will adjust the size of the image to not be larger than the bounds of the button. Also set the image as the background Image of the button instead of the image.
objectButton.contentMode = UIViewContentModeScaleAspectFit;
[objectButton setBackgroundImage:image forStage:UIControlStateNormal];

how to reduce imagesize in iphone

Iam new to iphone currently my problem is
i displayed 6 images in image view It was not showing images in imageview I think the imageviews are in large size
so plz send me code for this imageview.
Thank u in advance.
You could use this for image resizing:
- (UIImage *)rescaleImageToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resImage;
}