Macos 在mac os x 10.11中将图像写入目标路径引发错误
我正在尝试将图像写入特定路径。为此,我编写的代码是:Macos 在mac os x 10.11中将图像写入目标路径引发错误,macos,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 rel
- (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才会出现这种情况
任何人都可以对此提出建议。我强烈怀疑以下代码:
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];
应用程序是沙盒吗?是的,应用程序是沙盒。是的,我调试了它。。。而此方法CGImageDestinationCreateWithURL返回nil。你能给我建议一个构造outurlOK的最佳方法吗?好的,我已经尝试了一个答案。可能会解决问题,也可能不会,但它比您目前拥有的要好。即使在代码重构之后,崩溃仍然存在;