2012-11-10 40 views
0

我试图生成一个qr并将其导出到PNG,但是,只要我这样做,它会将其导出为模糊图像。我不确定我能做些什么来使它不模糊。当我将文件导出 导出的PNG模糊

这里是我使用的代码

  1. 在预览窗口中
  2. 它发生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]; 
        } 
    } 
    

回答

0

几乎可以肯定,当图像缩放img = [self scaleImageTo:img size:newSize];而不是做你应该重现QR图像中的正确尺寸显示/打印/输出的模糊介绍/等等