2011-06-21 101 views
0

我正在開發一個項目,在該項目中我必須裁剪圖像並將裁剪後的圖像保存在照片庫中。我在裁剪圖像方面取得了成功,但問題在於,當我裁剪圖像並顯示圖像時,它被拉伸了。我也在使用縱橫比模式。但它仍然是stretched.Here代碼如何調整Uiimage的高度和寬度比率

-(void)cropLassoArea { 

isLassoSelected = NO; 
lassoStyle = 0; 
CGRect frame; 
frame = mainImageView.frame; 
mainImageView.frame.size = CroppedImage.size; 
for (int i = 0; i < [lassoArray count]; i++) { 

    CAShapeLayer *layer = [lassoArray objectAtIndex:i]; 

    CGPathRef myPath = layer.path; 
    UIGraphicsBeginImageContext(mainImageView.frame.size); 

    [mainImageView setContentMode:UIViewContentModeCenter]; 


    [mainImageView.image drawInRect:CGRectMake(0, 0,CroppedImage.size.width,CroppedImage.size.height)]; 

    context = UIGraphicsGetCurrentContext(); 
    CGContextBeginPath(context); 
    CGContextAddPath(context, myPath); 

    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor whiteColor].CGColor); 
    CGContextSetBlendMode(context, kCGBlendModeClear); 
    CGContextDrawPath(context, kCGPathFill); 
    CGContextEOFillPath(context); 

    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
    CGContextSaveGState(context); 
    UIGraphicsEndImageContext(); 

} 

}

回答

0

我建議你從this位置下載文件,我建議你去通過他們的教程。它很容易,它不會拉伸你的形象。我在我的一個工作項目中使用了這些文件。你只需要導入他們的頭文件,並撥打以下行

UIImage *tmpImage = [yourImageView.image resizedImage:CGSizeMake(desiredWidth,DesiredHeight) interpolationQuality:kCGInterpolationHigh]; 
    yourImageView.contentMode = UIViewContentModeCenter; 
    [yourImageView setImage:tmpImage]; 

希望它有幫助。

相關問題