您可以使用此功能來創建一個播種的版本交接按鈕的高度和寬度參數
- (UIImage*)thumbnailOfHeight:(int)height andWidth:(int)width fromImage:(UIImage*)image{
UIImage *thumbnail;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
BOOL isWidthGreaterThanHeight = (image.size.width > image.size.height);
float sideFull = (isWidthGreaterThanHeight)? image.size.height : image.size.width;
CGRect clippedRect = CGRectMake(0, 0, sideFull, sideFull);
UIGraphicsBeginImageContext(CGSizeMake(height, width));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextClipToRect(currentContext, clippedRect);
CGFloat scaleFactor = width/sideFull;
if (isWidthGreaterThanHeight) {
CGContextTranslateCTM(currentContext, -((image.size.width - sideFull)/2)*scaleFactor, 0);
}
else {
CGContextTranslateCTM(currentContext,0, -((image.size.height - sideFull)/2)*scaleFactor);
}
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);
[imageView.layer renderInContext:currentContext];
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[imageView release];
return thumbnail;
}
非常感謝您爲此片段!我將不得不看看這個UIGraphicsContext的東西,然後做一些圖像處理的東西。 –
嗯...有沒有我需要爲CALayer做的任何導入? LLVM抱怨'imageView.layer'說這是CALayer的前向聲明。 –
是的QuartzCore框架 –