gpt4 book ai didi

iPhone NSBitmapImageRep

转载 作者:行者123 更新时间:2023-12-03 17:45:50 25 4
gpt4 key购买 nike

我有一个包含 JPEG 图像数据的缓冲区。我需要在 UIImageView 中显示此图像。我需要将此图像缓冲区转换为 UIImage 对象并按如下方式使用它

NSData *data = [NSData dataWithContentsOfFile:appFile];UIImage *theImage = [[UIImage alloc] initWithData:data];

我显示了图像,但与实际分辨率相比,分辨率较低。我是否需要先将其转换为 Bitmap,然后将其与 UIImage 一起使用?我似乎无法使用 NSBitmapImageRep。关于如何实现这一目标有什么想法吗?

最佳答案

如果 UIImageView 框架尺寸与源图像尺寸不同,您将获得图像的调整大小版本。质量可能相当粗糙,具体取决于执行的转换量。

我在网上某个地方找到了这段代码(对不起原作者 - 我已经失去了归属),它执行更平滑的调整大小:

UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;

// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width, // width
thumbRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
4 * thumbRect.size.width, // rowbytes
CGImageGetColorSpace(imageRef),
alphaInfo
);

// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);

// Get an image from the context and a UIImage
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* result = [UIImage imageWithCGImage:ref];

CGContextRelease(bitmap); // ok if NULL
CGImageRelease(ref);

return result;
}

关于iPhone NSBitmapImageRep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/801910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com