我一直在this this for for for for for for and and and while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while while
我认为,在我的设计的概念阶段提前,从iPhone的相机或库中获取图像,将其缩小到指定高度,使用与Aspect Fill选项相当的功能将是一件小事. UIImageView(完全在代码中),然后裁掉任何不适合传递给CGRect的东西.
从相机或图书馆获取原始图像非常简单.我很惊讶其他两个步骤的难度.
附图显示了我想要实现的目标.有人请你好好握住我的手吗?到目前为止我发现的每个代码示例似乎都会破坏图像,颠倒过来,看起来像废话,画出界限,或者其他方法都不能正常工作.
我需要同样的东西 - 在我的情况下,选择适合缩放的尺寸,然后裁剪每一端以使其余部分适合宽度.(我在横向工作,所以可能没有注意到肖像模式的任何缺陷.)这是我的代码 - 它是UIImage的一个类似的一部分.我的代码中的目标大小始终设置为设备的全屏大小.
@implementation UIImage (Extras) #pragma mark - #pragma mark Scale and crop image - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize { UIImage *sourceImage = self; 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大小的方法的代码.相关部分如下:
+ (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 *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize { //If scaleFactor is not touched, no scaling will occur CGFloat scaleFactor = 1.0; //Deciding which factor to use to scale the image (factor = targetSize / imageSize) if (image.size.width > targetSize.width || image.size.height > targetSize.height) if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or scaleFactor = targetSize.height / image.size.height; // scale to fit heigth. UIGraphicsBeginImageContext(targetSize); //Creating the rect where the scaled image is drawn in CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2, (targetSize.height - image.size.height * scaleFactor) / 2, image.size.width * scaleFactor, image.size.height * scaleFactor); //Draw the image into the rect [image drawInRect:rect]; //Saving the image, ending image context UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
我提出这个.她不是美女吗?;)
有一个很大的代码与调整图像大小和其他一些操作有关.我试图想象你如何调整图像大小时来到这个... http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
干得好.这个很完美;-)
编辑:见下面的评论 - "不适用于某些图像,失败:CGContextSetInterpolationQuality:无效的上下文0x0错误"
// Resizes the image according to the given content mode, taking into account the image's orientation - (UIImage *)resizedImageWithContentMode:(UIViewContentMode)contentMode imageToScale:(UIImage*)imageToScale bounds:(CGSize)bounds interpolationQuality:(CGInterpolationQuality)quality { //Get the size we want to scale it to CGFloat horizontalRatio = bounds.width / imageToScale.size.width; CGFloat verticalRatio = bounds.height / imageToScale.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]; } //...and here it is CGSize newSize = CGSizeMake(imageToScale.size.width * ratio, imageToScale.size.height * ratio); //start scaling it CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); CGImageRef imageRef = imageToScale.CGImage; CGContextRef bitmap = CGBitmapContextCreate(NULL, newRect.size.width, newRect.size.height, CGImageGetBitsPerComponent(imageRef), 0, CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef)); CGContextSetInterpolationQuality(bitmap, quality); // Draw into the context; this scales the image CGContextDrawImage(bitmap, newRect, imageRef); // Get the resized image from the context and a UIImage CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap); UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; // Clean up CGContextRelease(bitmap); CGImageRelease(newImageRef); return newImage; }
这是Jane Sales在Swift中的回答版本.干杯!
public func resizeImage(image: UIImage, size: CGSize) -> UIImage? { var returnImage: UIImage? var scaleFactor: CGFloat = 1.0 var scaledWidth = size.width var scaledHeight = size.height var thumbnailPoint = CGPointMake(0, 0) if !CGSizeEqualToSize(image.size, size) { let widthFactor = size.width / image.size.width let heightFactor = size.height / image.size.height if widthFactor > heightFactor { scaleFactor = widthFactor } else { scaleFactor = heightFactor } scaledWidth = image.size.width * scaleFactor scaledHeight = image.size.height * scaleFactor if widthFactor > heightFactor { thumbnailPoint.y = (size.height - scaledHeight) * 0.5 } else if widthFactor < heightFactor { thumbnailPoint.x = (size.width - scaledWidth) * 0.5 } } UIGraphicsBeginImageContextWithOptions(size, true, 0) var thumbnailRect = CGRectZero thumbnailRect.origin = thumbnailPoint thumbnailRect.size.width = scaledWidth thumbnailRect.size.height = scaledHeight image.drawInRect(thumbnailRect) returnImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return returnImage }