2011-07-22 86 views
2

我使用尺寸爲80X80的imagview來顯示大圖像(1024 X 780)。如何將大圖像與原始圖像的質量保持一致?

將大圖放入imageview中時,圖像看起來受到擠壓,壓縮的東西不像質量那樣。

我的問題是,我怎樣才能使大圖像變成小而質量與原始圖像一樣好?

回答

0

從1034x780進入80x80幾乎不可能保證質量,因爲沒有足夠的空間來捕捉所有細節。

您可以嘗試它的規模:

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 
+0

謝謝q這個編碼是使用完整的我的概念。 – Durga

+0

這可以工作,但在iOS5.0.1中會導致內存泄漏。有其他方法嗎? http://stackoverflow.com/questions/8236837/memory-leak-drawinrect-on-ios5-0-1 – David

1

在圖像視圖,請進行imageview.mode中心沒有規模,以適應或任何其他。 我相信這將把你的任何縮放圖像最好質量在你的80x80尺寸。

1

在這種情況下,你應該做的圖像進行適當縮放,美要在ImageView的顯示

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize 
{ 
    UIImage *sourceImage = chosenImage; 
    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; 
     else 
      scaleFactor = heightFactor; 

     scaledWidth = width * scaleFactor*1; 
     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); 

    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 

    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage ; 
} 
+0

比q surya這個編碼是有用的我的概念 – Durga

0

可以設置圖像模式[imageView setContentMode:UIViewContentModeScaleAspectFill]; imageView.autoresizingMask =(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); [imageView setClipsToBounds:YES];

相關問題