可能重複:
The simplest way to resize an UIImage?從ios設備拍攝照片後可以調整圖像大小嗎?
我使用iPod設備的攝像頭捕捉圖像和拍攝圖像後,我想調整它按在我的應用程序的用戶需求。但是通過在攝像頭代碼中啓用編輯:true,它僅以圖像的中間部分的矩形格式顯示圖像。提前感謝您的幫助。請注意,我希望圖像在這裏進行克隆。它意味着圖像會根據croaped圖像調整大小。
可能重複:
The simplest way to resize an UIImage?從ios設備拍攝照片後可以調整圖像大小嗎?
我使用iPod設備的攝像頭捕捉圖像和拍攝圖像後,我想調整它按在我的應用程序的用戶需求。但是通過在攝像頭代碼中啓用編輯:true,它僅以圖像的中間部分的矩形格式顯示圖像。提前感謝您的幫助。請注意,我希望圖像在這裏進行克隆。它意味着圖像會根據croaped圖像調整大小。
這可能會幫助你
- (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;
}
在這裏你可以做來調整圖像。
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;
}
希望,它會幫助你。
你必須導入兩個類,即UIImage Resize.h和.m文件.....這些文件對調整圖片大小非常有幫助 –
好的。但是你能詳細告訴我添加這些文件嗎?我的意思是我必須爲此導入什麼框架或文件。感謝您的快速回復。 – Arun
你也可以嘗試這個鏈接可能會幫助你..... http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage –