将图像写入目标路径,在mac os x 10.11中抛出错误

时间:2015-10-09 10:03:12

标签: macos

我正在尝试将图像写入特定路径。因为我写的代码是:

- (void)thumbnailWithDataProvider:(CGDataProviderRef)dataProvider url:(NSURL *)url guid:(NSString *)guid {
    // The caller of this method typically releases this strait after calling.
    // We therefore retain it and release it at the end of the block.
    CGDataProviderRetain(dataProvider);

    // Dispatch the generation in a block on a queue sutable for this guid
    dispatch_async([self queueForGuid:guid], ^{
        NRLog(@"PDFORDER: Generate start %@ %@", guid, url);

        CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithProvider(dataProvider);
        size_t numPages = CGPDFDocumentGetNumberOfPages(documentRef);
        if ( numPages ) {
            CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1);
            CGRect cropBox = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
            for ( size_t i = 0; i < NRMThumbnailCount; i++ ) {
                size_t scale = NRMThumbnailSizes[i];
                NSString *path = [url path];
                path = [NSString stringWithFormat:@"%@%lu.png", path, scale];
                NSURL *outurl = [NSURL fileURLWithPath:path];
                CGImageRef imageRef;

                CGFloat scaleX = scale/cropBox.size.width;
                CGFloat scaleY = scale/cropBox.size.height;
                CGFloat pdfScale = ( scaleX < scaleY ? scaleX : scaleY );
                CGFloat width = (CGFloat)ceil((double) pdfScale*cropBox.size.width);
                CGFloat height = (CGFloat)ceil((double) pdfScale*cropBox.size.height);
                CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
                CGContextRef context = CGBitmapContextCreate(NULL, width,  height, 8, width*4, colorSpace, kCGImageAlphaPremultipliedLast);
                CGColorSpaceRelease(colorSpace);
                CGContextScaleCTM(context, pdfScale, pdfScale);
                CGContextSetFillColor(context,  NRMPDFBackgroundColorComponents);
                CGContextFillRect(context, cropBox);
                CGContextDrawPDFPage( context, pageRef );
                imageRef = CGBitmapContextCreateImage(context);
                CGContextRelease(context);

                CGImageDestinationRef imageDest = CGImageDestinationCreateWithURL((CFURLRef)outurl, THUMBNAIL_TYPE, 1, NULL);//Getting error at this line as " <Error>: ImageIO: CGImageDestinationSetProperties image destination parameter is nil " and the app getting crashed.
                if(!imageDest) {
                    NSLog(@"***Could not create image destination ***");
                }
                CFStringRef keys[1];
                keys[0] = kCGImageDestinationLossyCompressionQuality;

                CFNumberRef values[1];
                CGFloat compression = (CGFloat)THUMBNAIL_COMPRESSION;
                CFNumberRef compressionNumber = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &compression);
                values[0] = compressionNumber;

                CFDictionaryRef properties = CFDictionaryCreate(kCFAllocatorDefault, (void *)keys, (void *)values, 1, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

                CGImageDestinationSetProperties(imageDest, properties);

                CGImageDestinationAddImage(imageDest, imageRef, NULL);
                CGImageDestinationFinalize(imageDest);
                CGImageRelease(imageRef);
                CFSafeRelease(imageDest);
                CFSafeRelease(compressionNumber);
                CFSafeRelease(properties);
            }
            [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadWithName:kNRMPDFThumbnailImageChangeNotification object:guid];
        }
        CGPDFDocumentRelease(documentRef);
        CGDataProviderRelease(dataProvider);
        NRLog(@"PDFORDER: Generate end %@ %@", guid, url);
    });
}

我传递给方法的所有参数的值仍然是目标值变为零。

仅在Mac OS X 10.11上发生这种情况

任何人都可以建议。

1 个答案:

答案 0 :(得分:0)

我强烈怀疑这段代码:

NSString *path = [url path];
path = [NSString stringWithFormat:@"%@%lu.png", path, scale];
NSURL *outurl = [NSURL fileURLWithPath:path];

更好的是:

NSString *filename = [NSString stringWithFormat:@"%lu.png", scale];
NSURL *outurl = [url URLByAppendingPathComponent:filename];