我目前正在使用不模糊的代码,但它只是削减了一块,如果我尝试在线使用不同的代码,它会削减一块,它也变得模糊。我需要做什么,图片是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
答案 0 :(得分: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;
}];
答案 1 :(得分: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?
答案 2 :(得分: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;