0

Possible Duplicate:
The simplest way to resize an UIImage?

I am using camera of ipod device to capture image and after taking image i want to resize it as per user need in my application . But by enabling editing : true in camera code , it shows the image in rectangle format of the image's middle section only.Thanks in advance for help.Note that I want image croapping here. it means image accutaly resize according to croaped image.

Community
  • 1
  • 1
Arun
  • 163
  • 10
  • 1
    You have to import two classes that is UIImage Resize.h and .m file.....these files are very helpful to resize the picture – Sudha Tiwari Dec 28 '12 at 07:24
  • Ok. But can you tell me in details for adding these files. I mean what framework or files i have to import for that. Thanks for your quick responce. – Arun Dec 28 '12 at 07:29
  • You can also try this link may be it will help you..... http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage – Sudha Tiwari Dec 28 '12 at 07:30
  • follow my answer for crop image http://stackoverflow.com/questions/13890299/uiimageview-setframe-not-working-in-uitableviewcell/13892709#13892709 – Rajneesh071 Dec 28 '12 at 07:43

2 Answers2

1

this may help you

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize AndImage:(UIImage*)sourceImage
{
UIImage *newImage = nil;        
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
{
    CGFloat widthFactor = targetWidth / width;
    CGFloat heightFactor = targetHeight / height;

    if (widthFactor > heightFactor) 
        scaleFactor = widthFactor; // scale to fit height
    else
        scaleFactor = heightFactor; // scale to fit width.

    scaledWidth  = width * scaleFactor;
    scaledHeight = height * scaleFactor;

    // center the image
    if (widthFactor > heightFactor)
    {
        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
    }
    else 
        if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
}       

UIGraphicsBeginImageContext(targetSize); // this will crop

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil) 
NSLog(@"could not scale image");

//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
1

Here you can do to resize image.

UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
CGImageRef          imageRef = [inImage CGImage];
CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;

// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
            NULL,
            thumbRect.size.width,       // width
            thumbRect.size.height,      // height
            CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
            4 * thumbRect.size.width,   // rowbytes
            CGImageGetColorSpace(imageRef),
            alphaInfo
    );

// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);

// Get an image from the context and a UIImage
CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
UIImage*    result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap);   // ok if NULL
CGImageRelease(ref);

return result;

}

Hope, it'll help.

josh
  • 1,681
  • 4
  • 28
  • 61