【问题标题】:Exported PNG is blurry导出的 PNG 模糊
【发布时间】:2012-11-10 22:11:16
【问题描述】:

我正在尝试生成 qr 并将其导出为 PNG,但每当我这样做时,它都会将其导出为模糊图像。我不确定我能做些什么使它不模糊。

有两种情况:

  1. 在预览窗口中
  2. 当我导出文件时

这是我正在使用的代码。任何帮助或建议将不胜感激,因为我对这门语言还是新手并试图弄清楚。

我的代码来了:

- (NSImage *)scaleImageTo:(NSImage *)inputImage size:(NSSize)newSize
{
    NSImage *cropped = [[NSImage alloc] initWithSize:NSMakeSize(newSize.width, newSize.height)];

     [cropped lockFocus];
     [inputImage drawInRect:NSMakeRect(0,0,cropped.size.width,cropped.size.height)
                fromRect:NSMakeRect(0,0,inputImage.size.width,inputImage.size.height)
               operation:NSCompositeCopy
                fraction:1.0];
     [cropped unlockFocus];


     NSRect rect = NSMakeRect(0, 0, cropped.size.width, cropped.size.height);

     CIImage* ciImage1 = [[CIImage alloc] initWithData:[cropped TIFFRepresentation]];


    [[NSGraphicsContext currentContext] setShouldAntialias: NO];

     NSImage *buffer2 = [[NSImage alloc] initWithSize:NSMakeSize([cropped size].width , [cropped size].height)];

    [buffer2 lockFocus];
    CIFilter* filter2 = [CIFilter filterWithName:@"CIMaskToAlpha"];
    [filter2 setValue:ciImage1 forKey:@"inputImage"];

    CIImage* output2 = [filter2 valueForKey:@"outputImage"];

    [output2 drawAtPoint:NSZeroPoint fromRect:rect operation:NSCompositeSourceOver fraction:1.0];


    //background
    [[_backgroundColorSelector color] set];
    NSRectFillUsingOperation(rect, NSCompositeSourceAtop);
    //foreground
    [[_foregroundColorSelector color] set];
    NSRectFillUsingOperation(rect, NSCompositeDestinationAtop);

    [buffer2 unlockFocus];
    return buffer2;
}
- (void)writeImageAt:(NSImage *)img path:(NSString *)strPath
{
    //create a NSBitmapImageRep
     NSBitmapImageRep *bmpImageRep = [[NSBitmapImageRep alloc]initWithData:[img TIFFRepresentation]];
     //add the NSBitmapImage to the representation list of the target
     [img addRepresentation:bmpImageRep];

     //get the data from the representation
     NSData *data = [bmpImageRep representationUsingType: NSPNGFileType
                                             properties: nil];
    [data writeToFile:strPath atomically:YES];
}
- (void)savePNGAt:(NSImage *)img path:(NSString *)strPath
{
    NSString * qrWidth = [_qrWidth stringValue];
    NSString * qrHeight = [_qrHeight stringValue];

    if (qrHeight != nil && ![qrWidth isEqualToString:@""] && qrWidth != nil && ![qrHeight isEqualToString:@""]) {
        NSSize newSize = NSMakeSize([qrWidth floatValue], [qrHeight floatValue]);
        img = [self scaleImageTo:img size:newSize];
        [self writeImageAt:img path:strPath];
    }
    else
    {
        img = [self changeImageColor:img];
        [self writeImageAt:img path:strPath];
    }
}

【问题讨论】:

    标签: objective-c cocoa xcode4.5


    【解决方案1】:

    几乎可以肯定,当图像被缩放img = [self scaleImageTo:img size:newSize]; 时会引入模糊,而不是这样做,您应该以正确的尺寸复制 QR 图像以显示/打印/导出/等。

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 2011-10-22
      • 2011-09-30
      • 1970-01-01
      相关资源
      最近更新 更多