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 關注
- 719 瀏覽
添加回答
舉報
0/150
提交
取消
