如何在iPhone中通过objective-c以编程方式调整图像大小
我有一个应用程序,我在一个小空间显示大图像。 图像是相当大的,但我只显示在100×100像素的帧。 我的应用程序响应缓慢,因为我使用的图像的大小。
为了提高性能,我如何使用Objective-C以编程方式调整图像大小?
请find下面的代码。
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size { UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return destImage; }
我最喜欢的方式是使用CGImageSourceCreateThumbnailAtIndex
(在ImageIO框架中)。 这个名字有点误导。
下面是我最近的一个应用程序的一些代码的摘录。
CGFloat maxw = // whatever; CGFloat maxh = // whatever; CGImageSourceRef src = NULL; if ([imageSource isKindOfClass:[NSURL class]]) src = CGImageSourceCreateWithURL((__bridge CFURLRef)imageSource, nil); else if ([imageSource isKindOfClass:[NSData class]]) src = CGImageSourceCreateWithData((__bridge CFDataRef)imageSource, nil); // if at double resolution, double the thumbnail size and use double-resolution image CGFloat scale = 1; if ([[UIScreen mainScreen] scale] > 1.0) { scale = 2; maxw *= 2; maxh *= 2; } // load the image at the desired size NSDictionary* d = @{ (id)kCGImageSourceShouldAllowFloat: (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform: (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways: (id)kCFBooleanTrue, (id)kCGImageSourceThumbnailMaxPixelSize: @((int)(maxw > maxh ? maxw : maxh)) }; CGImageRef imref = CGImageSourceCreateThumbnailAtIndex(src, 0, (__bridge CFDictionaryRef)d); if (NULL != src) CFRelease(src); UIImage* im = [UIImage imageWithCGImage:imref scale:scale orientation:UIImageOrientationUp]; if (NULL != imref) CFRelease(imref);
此代码仅用于更改图像缩放而不用于resize您必须将CGSize设置为您的图像宽度和高度,以便图像不会伸展并排列在中间。
- (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size { CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height); CGFloat width = image.size.width * scale; CGFloat height = image.size.height * scale; CGRect imageRect = CGRectMake((size.width - width)/2.0f, (size.height - height)/2.0f, width, height); UIGraphicsBeginImageContextWithOptions(size, NO, 0); [image drawInRect:imageRect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
如果您使用不同尺寸的图片,并且每次resize都会降低应用的性能。 解决scheme是不调整它们只是使用button代替imageview。 只需设置button上的图像,它会自动resize,你会得到很好的performance。
我也在调整图像的同时将其设置在单元格上,但我的应用程序变得缓慢因此,我用Button代替imageview(不调整图像编程方式button是做这个工作),它工作得很好。
-(UIImage *)scaleImage:(UIImage *)image toSize:. (CGSize)targetSize { //If scaleFactor is not touched, no scaling will occur CGFloat scaleFactor = 1.0; //Deciding which factor to use to scale the image (factor = targetSize / imageSize) if (image.size.width > targetSize.width || image.size.height > targetSize.height || image.size.width == image.size.height) if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.
由于代码在iOS 4中运行得非常好,为了向后兼容,我添加了对操作系统版本的检查,对于低于5.0的任何代码,旧代码都可以工作。
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality { BOOL drawTransposed; CGAffineTransform transform = CGAffineTransformIdentity; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) { // Apprently in iOS 5 the image is already correctly rotated, so we don't need to rotate it manually drawTransposed = NO; } else { switch (self.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: drawTransposed = YES; break; default: drawTransposed = NO; } transform = [self transformForOrientation:newSize]; } return [self resizedImage:newSize transform:transform drawTransposed:drawTransposed interpolationQuality:quality]; }
你可以使用这个。
[m_Image.layer setMinificationFilter:kCAFilterTrilinear];