【问题标题】:NSBitmapImageRep resizingNSBitmapImageRep 调整大小
【发布时间】:2012-08-16 13:00:45
【问题描述】:

有没有办法调整NSBitmapImageRep 的大小?

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

如果没有,那我该怎么办?

我需要在将图像写入文件之前调整其大小,我的图像来自NSBitmapImageRep。当然,如果调整大小更容易,我可以将其转换为NSImageCIImage。如果是,那么请告诉我。

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

【问题讨论】:

    标签: objective-c macos image bitmap resize


    【解决方案1】:

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

    - (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 data
    

    【讨论】:

    • 正如我之前提到的I need to have ability to resize image without maintaining proportions,我猜你建议的方法保持比例。
    • 它返回大小合适的图像,但图像是空的。问题出在哪里?
    【解决方案2】:

    您应该使用正确插入源代码的实现。

    您可以使用目标大小(例如 3200x3200)和规格的 CGBitmapContext 来完成此操作,然后将源图像图像绘制到 CGBitmapContext。然后,您可以使用 CGBitmapContext 的图像创建函数,或者使用上下文的位图缓冲区作为输出样本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 2010-12-11
      • 2016-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多