我在UIImage + Resize Category扩展here中使用UIImage类的类扩展名来解决一些错误。我正在使用Xcode 5.1.1。
问题:有没有更好的方法在 Quartz2D 或其他一些框架中使用其他东西来解决这些问题,以及将来证明UIImage的类别扩展?
这是控制台输出:
我正在抓取imageView图像并调用方法:
if (image != nil) {
//present our image in the image view
_imageView.image = image;
//make half size thumbnail.
CGSize imageSize = CGSizeMake(300,300);
image = [image resizedImage:imageSize interpolationQuality:kCGInterpolationHigh];
}
以下是包含所有问题的类别扩展私有助手方法:
// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
// If the new size is not integral, it will be rounded up
- (UIImage *)resizedImage:(CGSize)newSize
transform:(CGAffineTransform)transform
drawTransposed:(BOOL)transpose
interpolationQuality:(CGInterpolationQuality)quality {
CGFloat scale = MAX(1.0f, self.scale);
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width*scale, newSize.height*scale));
CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
CGImageRef imageRef = self.CGImage;
// Fix for a colorspace / transparency issue that affects some types of
// images. See here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/comment-page-2/#comment-39951
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
newRect.size.width,
newRect.size.height,
8, /* bits per channel */
(newRect.size.width * 4), /* 4 channels per pixel * numPixels/row */
colorSpace,
kCGBitmapByteOrderDefault
);
CGColorSpaceRelease(colorSpace);
// Rotate and/or flip the image if required by its orientation
CGContextConcatCTM(bitmap, transform);
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, quality);
// Draw into the context; this scales the image
CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:self.scale orientation:UIImageOrientationUp];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(newImageRef);
return newImage;
}
答案 0 :(得分:1)
在使用Quartz的 CGBitmapContextCreate 进行一些调整之后,我能够解决已解决的问题。
有一些" braniacs"在那里谁可以解释为什么这有效,但从iOS7和现在最近,iOS8(Xcode 6.0),这个代码适合我。下面是我更改的代码:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
newRect.size.width,
newRect.size.height,
CGImageGetBitsPerComponent(imageRef), /* bits per channel */
(newRect.size.width * 4), /* 4 channels per pixel * numPixels/row */
colorSpace,
CGImageGetBitmapInfo(imageRef)/*SOLVED: instead of a constant here add this CGImageGetBitmapInfo(imageRef)*/
);
CGColorSpaceRelease(colorSpace);
我必须更改某些内容,例如硬编码的地方( size_t bitsPerComponent )我使用 CGImageGetBitsPerComponent ( imageRef ),因为我想要它无论 imageRef 输入的是8位还是每个组件的位数。接下来的一点(双关语)是解决胡思乱想的" kCGBitmapByteOrderDefault "这是吹动块,但是这次我发现动态是有用的所以我使用 CGImageGetBitmapInfo ( imageRef )返回一个常量(返回位图图像的位图信息) )指定位图数据的类型以及alpha通道是否在数据中。
我希望这有助于某人,我欢迎任何人帮助更好地解释这一点,因为我知道有更好的人在这里做。干杯!