I am picking an image from image picker and then i want to change ratios for that image as facebook cover. i have an image let suppose its resolution ia 640 widht and 480 height and i want to change it for facebook cover(851 pixels wide and 315 pixels tall) how will i do that programmatically in iphone
Check this link for cover picture details
Thanks.
Use this Method to Resize the image according to the given content mode, taking into account the image's orientation...
- (UIImage *)resizedImageWithContentMode:(UIViewContentMode)contentMode bounds:(CGSize)bounds
interpolationQuality:(CGInterpolationQuality)quality {
CGFloat horizontalRatio = bounds.width / self.size.width;
CGFloat verticalRatio = bounds.height / self.size.height;
CGFloat ratio;
switch (contentMode) {
case UIViewContentModeScaleAspectFill:
ratio = MAX(horizontalRatio, verticalRatio);
break;
case UIViewContentModeScaleAspectFit:
ratio = MIN(horizontalRatio, verticalRatio);
break;
default:
[NSException raise:NSInvalidArgumentException format:#"Unsupported content mode: %d", contentMode];
}
CGSize newSize = CGSizeMake(self.size.width * ratio, self.size.height * ratio);
return [self resizedImage:newSize interpolationQuality:quality];
}
and go this this link for more detail,Resizing Uiimage in right way .
Related
I've an UIImageView with content mode Aspect Fit of size 220x155. I'm dynamically inserting different images in different resolutions, but all larger than the size of the UIImageView. As the content mode is set to Aspect Fit, the image is scaled with respect to the ratio to fit the UIImageView.
My problem is, that if for instance the image inside the UIImageView is scaled to 220x100, I would like the UIImageView to shrink from a height of 155 to 100 too to avoid space between my elements.
How can I do this?
I wrote this method to get me the frame of the image view once it loaded an image.
So , the requirements for me were the same as in your case:
1) image view with aspect fit content mode
2) get the exact frame of the image ( this way you can re-position the image view )
Hope this helps:
- (CGRect) getFrameOfImage:(AsyncImageView *) imgView
{
if(!imgView.loaded)
return CGRectZero;
CGSize imgSize = imgView.image.size;
CGSize frameSize = imgView.frame.size;
CGRect resultFrame;
if(imgSize.width < frameSize.width && imgSize.height < frameSize.height)
{
resultFrame.size = imgSize;
}
else
{
float widthRatio = imgSize.width / frameSize.width;
float heightRatio = imgSize.height / frameSize.height;
float maxRatio = MAX (widthRatio , heightRatio);
NSLog(#"widthRatio = %.2f , heightRatio = %.2f , maxRatio = %.2f" , widthRatio , heightRatio , maxRatio);
resultFrame.size = CGSizeMake(imgSize.width / maxRatio, imgSize.height / maxRatio);
}
resultFrame.origin = CGPointMake(imgView.center.x - resultFrame.size.width/2 , imgView.center.y - resultFrame.size.height/2);
return resultFrame;
}
I am using here AsyncImageView but it will work just as good with UIImageView. The important thing to remember is to call this method AFTER the image was loaded.
Cheers!
Its very simple, you just need to get image actual size, which can be done by
UIImage *image = [UIImage imageName:#""];
then you just need to set frame
Like :-
imageView.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
Hope this helps you.
Once the imageview's image is set to the new image (and thus scaled) you can get the height of the image inside the imageview (imageview.image.size.height) and set the imageview's height (frame) accordingly.
i use this code to get image from facebook profile and show it on UIImageView
// Get the profile image
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:#"pic"]]]];
// Resize, crop the image to make sure it is square and renders
// well on Retina display
float ratio;
float delta;
float px = 100; // Double the pixels of the UIImageView (to render on Retina)
CGPoint offset;
CGSize size = image.size;
if (size.width > size.height) {
ratio = px / size.width;
delta = (ratio*size.width - ratio*size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = px / size.height;
delta = (ratio*size.height - ratio*size.width);
offset = CGPointMake(0, delta/2);
}
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * size.width) + delta,
(ratio * size.height) + delta);
UIGraphicsBeginImageContext(CGSizeMake(px, px));
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *imgThumb = UIGraphicsGetImageFromCurrentImageContext();
[imgThumb retain];
imageFb=imgThumb;
[profilePhotoImageView setImage:imgThumb];
I want to make a button, when clicked allows the user to change the image by an image in the library of the iPhone (photos he took with the iphone ..) . I have no idea how I should proceed, help please
Access to the user's photo library is most easily mediated with the use of UIImagePickerController. You create and configure and instance, set a delegate and display it modally. Implement -imagePickerController:didFinishPickingMediaWithInfo to be notified when the user selects an image. You can then persist this image in the user's documents directory.
UIImagePickerController Class Reference
I have taken the image from the iPhone 4G and post those images to the server, after that i fetched those images and displayed in the table view cell. I want to display the correct aspect ratio of the image, when change the image height.
In my calculation,
CGSize imageSize = image.size;
CGFloat imageWidth = imageSize.width; (620) (In retina from iPhone 4G)
CGFloat imageHeight = imageSize.height; (620) (In retina from iPhone 4G)
CGFloat aspectRatio = imageSize.width / imageSize.height; (620 / 620 = 1).
CGFloat newImageWidth = 300.0f; (Fixed Width).
// Calculate new Image Height.
CGFloat newImageHeight = newImageWidth / aspectRatio; (300 / 1 = 300)
So, new image width / height.(300, 300).
And, Should i need to check the image is retina or not?
I don't know, whether the calculation is correct or not, so please guide me to achieve this.
I want to know, standard procedure to find the height of the image with the fixed image width(Set the correct aspect ratio).
Please help me out.
Thanks!
Why don't you just put it into a UIImageView and set its contentMode to UIViewContentModeScaleAspectFit?
i am getting a crash issue(it is due to low memory) while picking up the a large image (3.5 MB) from the photo library in iphone 3G. The problem is only with iphone 3G while it is working fine in iPhone 3GS and iPhone 4. I am saving this image to my iphone library from my email, and it seems that the image saved to the library is not getting optimized. Is this a bug ? Can anybody suggest me a solution. It is possible to restrict the user from picking very large image from the library in iphone 3G. I tried picking up this large image using the sample application given by apple, and it is crashing even with that.
Is the crash happening when you pick the image, or when you try to put the image in a UIImageView? If the latter, you may need to scale the image to a smaller resolution before placing it in the UIImageView. I have seen recommendations that image sizes greater than 1024 x 1024 should be avoided in UIImageView.
You can do this using code like the following:
if (image.size.width > 1024 || image.size.height > 1024) {
// resize the image
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = self.frame.size.width/self.frame.size.height;
if(imgRatio < maxRatio){
imgRatio = self.frame.size.height / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = self.frame.size.height;
} else {
imgRatio = self.frame.size.width / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = self.frame.size.width;
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
imageToDraw = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Note that imageToDraw is a UIImage * declared outside of this code, and is the final image that can be put into the UIImageView. This code came from a method in a custom view that I wrote, so where you see self.frame in the code, you can replace with whatever frame you are scaling the image into.
Given a UIImage of any dimension, I wish to generate a square "icon" sized version, px pixels to a side, without any distortion (stretching). However, I'm running into a little snag. Not quite sure where the problem is. Here's what I'm doing so far.
First, given a UImage size, I determine three things: the ratio to use when scaling down the image; a delta (the difference between our desired icon size and the longest side), and an offset (which is used to figure out our origin coordinate when clipping the image):
if (size.width > size.height) {
ratio = px / size.width;
delta = (ratio*size.width - ratio*size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = px / size.height;
delta = (ratio*size.height - ratio*size.width);
offset = CGPointMake(0, delta/2);
}
Now, let's say you have an image 640px wide by 480px high, and we want to get a 50px x 50px icon out of this. The width is greater than the height, so our calculations are:
ratio = 50px / 640px = 0.078125
delta = (ratio * 640px) - (ratio * 480px) = 50px - 37.5px = 12.5px
offset = {x=6.25, y=0}
Next, I create a CGRect rect that is large enough to be cropped down to our desired icon size without distortion, plus a clipRect for clipping purposes:
CGRect rect = CGRectMake(0.0, 0.0, (ratio * size.width) + delta,
(ratio * size.height) + delta);
CGRect clipRect = CGRectMake(offset.x, offset.y, px, px);
Substituting our values from above, we get:
rect = origin {x=0.0, y=0.0}, size {width=62.5, height=50.0}
clipRect = origin {x=6.25, y=0}, size {width=50.0, height=50.0}
So now we have a 62.5px wide by 50px high rect to work with, and a clipping rectangle that grabs the "middle" 50x50 portion.
On to the home stretch! Next, we set up our image context, draw the UIImage (called myImage here) into the rect, set the clipping rectangle, get the (presumably now-clipped) image, use it, and finally clean up our image context:
UIGraphicsBeginImageContext(rect.size);
[myImage drawInRect:rect];
UIRectClip(clipRect);
UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
// Do something with the icon here ...
UIGraphicsEndImageContext();
Only one problem: The clipping never occurs! I end up with an image 63px wide x 50px high. :(
Perhaps I'm misusing/misunderstanding UIRectClip? I've tried shuffling various things around: swapping the use of rect and clipRect, moving UIRectClip before drawInRect:. No dice.
I tried searching for an example of this method online as well, to no avail. For the record, UIRectClip is defined as:
Modifies the current clipping path by
intersecting it with the specified
rectangle.
Shuffling things around gets us a little bit closer:
UIGraphicsBeginImageContext(clipRect.size);
UIRectClip(rect);
[myImage drawInRect:rect];
Now we don't have distortion, but the clipped image isn't centered on the original as I expected. Still, at least the image is 50x50, though the variable names are now fouled up as a result of said shuffling. (I'll respectfully leave renaming as an exercise for the reader.)
Eureka! I had things a little mixed up. This works:
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * size.width) + delta,
(ratio * size.height) + delta);
UIGraphicsBeginImageContext(CGSizeMake(px, px));
UIRectClip(clipRect);
[myImage drawInRect:clipRect];
UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
// Do something with the icon here ...
UIGraphicsEndImageContext();
No more need for rect. The trick appears to be using a negative offset in the clipping rectangle, thereby lining up the origin of where we want to grab our 50 x 50 image (in this example).
Perhaps there's an easier way. If so, please weigh in!
I wanted to achieve a similar thing but found the answer from by the original poster didn't quite work. It distorted the image. This may well be solely because he didn't post the whole solution and had changed some of how the variables are initialised:
(if (size.width > size.height)
ratio = px / size.width;
Was wrong for my solution (which wanted to use the largest possible square from the source image). Also it is not necessary to use UIClipRect - if you make the context the size of the image you want to extract, no actual drawing will be done outside that rect anyway. It is just a matter of scaling the size of the image rect and offsetting one of the origin coordinates. I have posted my solution below:
+(UIImage *)makeIconImage:(UIImage *)image
{
CGFloat destSize = 400.0;
CGRect rect = CGRectMake(0, 0, destSize, destSize);
UIGraphicsBeginImageContext(rect.size);
if(image.size.width != image.size.height)
{
CGFloat ratio;
CGRect destRect;
if (image.size.width > image.size.height)
{
ratio = destSize / image.size.height;
CGFloat destWidth = image.size.width * ratio;
CGFloat destX = (destWidth - destSize) / 2.0;
destRect = CGRectMake(-destX, 0, destWidth, destSize);
}
else
{
ratio = destSize / image.size.width;
CGFloat destHeight = image.size.height * ratio;
CGFloat destY = (destHeight - destSize) / 2.0;
destRect = CGRectMake(0, destY, destSize, destHeight);
}
[image drawInRect:destRect];
}
else
{
[image drawInRect:rect];
}
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
wheeliebin answers is correct but he forgot a minus sign in front of destY
destRect = CGRectMake(0, -destY, destSize, destHeight);