有什么方法可以调整NSBitmapImageRep的大小吗?
我找到了setPixel:atX:y:方法,但我不确定它在做什么。这就是我需要的吗?
如果没有,我该怎么做呢?
在将图像写入文件之前,我需要调整图像的大小,而我的图像是NSBitmapImageRep格式的。当然,如果它更容易调整大小,我可以将其转换为NSImage或CIImage。如果是的话,那就让我知道。
顺便说一句,我需要有能力在不保持任何比例的情况下调整图像大小。例如,如果图像是3200x2000,我需要有能力调整它的大小100x100。我该怎么做呢?
发布于 2012-08-21 16:13:54
你应该使用一个能正确插入你的源代码的实现。
您可以使用目标大小(例如3200x3200)和规格的CGBitmapContext完成此操作,然后将源图像图像绘制到CGBitmapContext。然后,您可以使用CGBitmapContext的image create函数,也可以使用context的位图缓冲区作为输出样本。
发布于 2012-08-21 16:28:53
EDIT您可以使用以下功能调整图像大小,而无需保持任何比例:
- (NSImage *)imageResize:(NSImage*)anImage
newSize:(NSSize)newSize
{
NSImage *sourceImage = anImage;
[sourceImage setScalesWhenResized:YES];
// Report an error if the source isn't a valid image
if (![sourceImage isValid])
{
NSLog(@"Invalid Image");
} else
{
NSImage *smallImage = [[[NSImage alloc] initWithSize: newSize] autorelease];
[smallImage lockFocus];
[sourceImage setSize: newSize];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[sourceImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
[smallImage unlockFocus];
return smallImage;
}
return nil;
}第二,像这样保持比例:
NSData *imageData = [yourImg TIFFRepresentation]; // converting img into data
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; // converting into BitmapImageRep
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor]; // any number betwwen 0 to 1
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps]; // use NSPNGFileType if needed
NSImage *resizedImage = [[NSImage alloc] initWithData:imageData]; // image created from datahttps://stackoverflow.com/questions/12050517
复制相似问题