2014-04-28 136 views
1

我目前使用的代碼不是模糊的,但它只是剪掉一塊,如果我嘗試在網上使用不同的代碼,它會剪掉一塊,它也變得模糊。我需要做什麼,圖片是512x512,試圖把它降低到100x100,這是一個JPG格式。如何調整圖像大小並避免模糊?

這是我現在使用的代碼:

//setting up each cell 
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
        cellForItemAtIndexPath:(NSIndexPath *)indexPath 
    { 
     CollectionGridCell *myCell = [collectionView 
             dequeueReusableCellWithReuseIdentifier:@"GridCell" 
             forIndexPath:indexPath]; 


     long row = [indexPath row]; 

      NSURL *baseUrl = [NSURL URLWithString:@"http://example.com/"]; 

    NSString *imageItemName = [homeImages objectAtIndex:row]; 
    NSURL *url = [NSURL URLWithString:imageItemName relativeToURL:baseUrl]; 

    // NSURL *url = /* prepare a url... see note below */ 
    [myCell.homeImage setImageWithURL:url 
        placeholderImage:[UIImage imageNamed:@"menuButton.png"] 
          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { 
           // inspect cacheType here to make sure it's cached as you want it 
           myCell.homeImage.image = [self resizeImage:image newSize:CGSizeMake(75,75)]; 

          }]; 

    return myCell; 

} 


- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize { 
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
    CGImageRef imageRef = image.CGImage; 

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Set the quality level to use when rescaling 
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); 

    CGContextConcatCTM(context, flipVertical); 
    // Draw into the context; this scales the image 
    CGContextDrawImage(context, newRect, imageRef); 

    // Get the resized image from the context and a UIImage 
    CGImageRef newImageRef = CGBitmapContextCreateImage(context); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

    CGImageRelease(newImageRef); 
    UIGraphicsEndImageContext(); 

    return newImage; 

} 

更新:我試過甚至低於目前的那些每個方法,導致地修剪掉中間。我是否需要在代碼中指定其他內容,是否有一些東西叫它突出中間?

這是我的網格單元.h文件中:

#import <UIKit/UIKit.h> 

@interface CollectionGridCell : UICollectionViewCell 
@property (weak, nonatomic) IBOutlet UIImageView *homeImage; 

@end 

這是我的網格單元.m文件:

#import "CollectionGridCell.h" 

@implementation CollectionGridCell 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
} 
*/ 

@end 

回答

1

沒有必要重新調整圖像大小。只需將以下屬性設置爲圖像視圖即可。

[myCell.homeImage setImageWithURL:url 
        placeholderImage:[UIImage imageNamed:@"menuButton.png"] 
          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { 
           // inspect cacheType here to make sure it's cached as you want it 
           myCell.homeImage.image = image; 
           myCell.homeImage.contentMode = UIViewContentModeScaleAspectFit; 
          }]; 
+0

我在哪裏將這個權利包含在我調用resize方法的位置,因爲這樣做似乎不會縮小圖像的大小?我將如何指定我是否需要100x 100或50x50,即 – Lion789

+0

無需調用重新調整大小功能。查看更新後的答案。圖像將自動適應ImageView。 – Apurv

+0

好吧,但做這種方法似乎只是裁剪圖像的中間,並沒有顯示整個圖像只是更小... – Lion789

0

這裏有一個簡單的解決方案:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize { 
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
    CGImageRef imageRef = image.CGImage; 

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // Set the quality level to use when rescaling 
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); 

    CGContextConcatCTM(context, flipVertical); 
    // Draw into the context; this scales the image 
    CGContextDrawImage(context, newRect, imageRef); 

    // Get the resized image from the context and a UIImage 
    CGImageRef newImageRef = CGBitmapContextCreateImage(context); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

    CGImageRelease(newImageRef); 
    UIGraphicsEndImageContext();  

    return newImage; 
} 

推薦此鏈接: -

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

How to scale down a UIImage and make it crispy/sharp at the same time instead of blurry?

+0

是啊,這仍顯示爲模糊 – Lion789

0

方法:

CGRect rect = CGRectMake(0,0,100,100); 
UIGraphicsBeginImageContext(rect.size); 
[yourCurrentOriginalImage drawInRect:rect]; 
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

NSData *imageData = UIImagePNGRepresentation(picture1); 
UIImage *img=[UIImage imageWithData:imageData]; 

方法2:

CGSize newSize = CGSizeMake(100, 100); 
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; //image => Your imageName 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext(); 

ImageView.contentMode = UIViewContentModeScaleAspectFit;

+0

嘗試這不縮放圖像,只是切出一塊,所以我只看到一個100×100片的整體形象 – Lion789

+0

還是一樣的問題,但看我的U​​PDATE不知道在這一點上,是否需要在我的代碼中修復其他內容 – Lion789