UIImage:调整大小,然后裁剪我已经对这张脸猛击了好几天,尽管我一直觉得自己正处在启示录的边缘,但我根本无法实现我的目标。我认为,在我设计的概念阶段之前,从iPhone的相机或库中获取图像,将其缩小到指定的高度,使用与侧面填充选项UIImageView(完全在代码中),然后割掉任何不符合通过的CGRect的内容。从相机或图书馆获取原始图像是微不足道的。事实证明,其他两个步骤是多么困难,我对此感到震惊。附图显示了我想要达到的目标。有人能帮我握住我的手吗?到目前为止,我发现的每一个代码示例似乎都会破坏图像,使之颠倒,看起来像垃圾,超出界限,否则就不能正常工作。
3 回答
富国沪深
TA贡献1790条经验 获得超9个赞
@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;}
沧海一幻觉
TA贡献1824条经验 获得超5个赞
+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;{ UIGraphicsBeginImageContext( newSize ); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage;}
幕布斯7119047
TA贡献1794条经验 获得超8个赞
+ (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;}
- 3 回答
- 0 关注
- 665 浏览
添加回答
举报
0/150
提交
取消