首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSBitmapImageRep调整大小

NSBitmapImageRep调整大小
EN

Stack Overflow用户
提问于 2012-08-21 15:58:55
回答 2查看 1.3K关注 0票数 2

有什么方法可以调整NSBitmapImageRep的大小吗?

我找到了setPixel:atX:y:方法,但我不确定它在做什么。这就是我需要的吗?

如果没有,我该怎么做呢?

在将图像写入文件之前,我需要调整图像的大小,而我的图像是NSBitmapImageRep格式的。当然,如果它更容易调整大小,我可以将其转换为NSImageCIImage。如果是的话,那就让我知道。

顺便说一句,我需要有能力在不保持任何比例的情况下调整图像大小。例如,如果图像是3200x2000,我需要有能力调整它的大小100x100。我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2012-08-21 16:13:54

你应该使用一个能正确插入你的源代码的实现。

您可以使用目标大小(例如3200x3200)和规格的CGBitmapContext完成此操作,然后将源图像图像绘制到CGBitmapContext。然后,您可以使用CGBitmapContext的image create函数,也可以使用context的位图缓冲区作为输出样本。

票数 0
EN

Stack Overflow用户

发布于 2012-08-21 16:28:53

EDIT您可以使用以下功能调整图像大小,而无需保持任何比例:

代码语言:javascript
复制
- (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;
}

第二,像这样保持比例:

代码语言:javascript
复制
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 data
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12050517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档