是的。首先,你需要得到一個新的尺寸保持縱橫比不變:
CGFloat horizontalRatio = newSize.width/oldSize.width;
CGFloat verticalRatio = newSize.height/oldSize.height;
CGFloat ratio = MAX(horizontalRatio, verticalRatio);
CGSize newSize = CGSizeMake(oldSize.width *ratio, newSize.height*ratio)
然後,你需要重繪新的大小圖像(自我是你的形象):
UIGraphicsBeginImageContextWithOptions(newSize, YES, 0);
[self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然後裁剪至所需尺寸:
CGRect cropRect = CGRectMake ((newImage.size.width - width)/2, (newImage.size.height - height)/2, width, height)
UIGraphicsBeginImageContextWithOptions(cropRect.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect drawRect = CGRectMake(-cropRect.origin.x, -cropRect.origin.y, self.size.width, self.size.height);
CGContextClipToRect(context, CGRectMake(0, 0, cropRect.size.width, cropRect.size.height));
[self drawInRect:drawRect];
UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
正如我看到你發送的所有圖片鏈接似乎被裁剪到中間,也許有點重採樣 - 所以你可以看到更多的畫面,但它不是那麼討厭 – SergeS 2011-02-01 20:22:17
謝謝,SergeS。他們如何以編程方式執行此操作? – Crashalot 2011-02-01 23:25:27