将PDF转换为NSImageRep(png)时的彩色工件

时间:2014-05-05 17:18:00

标签: objective-c pdf png artifacts nsimagerep

我想将不同的PDF页面转换为png。之后,我遍历所有像素以搜索彩色像素。主要目标是获取PDF的彩色页面。 对于大多数页面,它运行得很好。但是在某些页面上,我将彩色文物用红色(留在字母旁边)和蓝色(在字母旁边) 这是PDF的原文: The original in the PDF http://brebook.de/images/pdf.png
这是png中转换后的字母: The original in the PDF http://brebook.de/images/png.png
我怎样才能防止这种丑陋的文物。我不能用这个彩色像素来表达我的全部想法。
这是将单个页面转换为png的代码:

//Path for saving colored Pages
NSURL *savePath = [self.homePath URLByAppendingPathComponent:@"brebook_temp/"];

//PDFDocument *thePDF = [[PDFDocument alloc] initWithURL:self.filename];
NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfURL:self.filename];

int coloredPages = 0;

for(int i = 0; i < self.getNumberOfPages; i++){
    @autoreleasepool {
        //set current page label to current page
        self.currentPage = i + 1;

        //set current page to i
        [img setCurrentPage:i];

        //create a new NSImage instance
        NSImage *singlePage = [NSImage new];

        //set actuall page
        [singlePage addRepresentation:img];

        //get "old" size
        NSSize oldSize = [singlePage size];

        //edit the size to 72dpi
        NSSize newSize;
        newSize.width = oldSize.width * 150/72;
        newSize.height = oldSize.height * 150/72;

        //make new image
        NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(newSize.width, newSize.height)];

        //write into new image
        [resizedImage lockFocus];
        [singlePage drawInRect: NSMakeRect(0, 0, newSize.width, newSize.height) fromRect: NSMakeRect(0, 0, oldSize.width, oldSize.height) operation: NSCompositeSourceOver fraction: 1.0];
        [resizedImage unlockFocus];

        //Set URL for single pages
        NSURL *pageFilename = [savePath URLByAppendingPathComponent: [NSString stringWithFormat:@"Seite_%d.png", i+1]];


        [[NSFileManager defaultManager] createFileAtPath: [pageFilename path]
                                                contents:[[NSBitmapImageRep imageRepWithData:[singlePage TIFFRepresentation]]
                                                          representationUsingType:NSPNGFileType properties:nil]
                                              attributes:nil];


        if([self getColoredPixelOfImageFromURL:pageFilename] > 0){
            coloredPages++;
            NSLog(@"SEITE %d -----------------------------------------------------------------------------------------------", i+1);
            _coloredPages = coloredPages;
            [self.coloredPagesList appendString:[NSString stringWithFormat:@"%d,",i+1]];
        }else{
            NSError *error = nil;
            [[NSFileManager defaultManager] removeItemAtURL:pageFilename error:&error];
            if(error){
                NSLog(@"%@", error);
            }
        }

        resizedImage = nil;
        singlePage = nil;

    }
}
[self.appTimer invalidate];

}

非常感谢您的帮助!!!

0 个答案:

没有答案