使用下面的代码我可以将pdf矢量图形图像添加到视图中。效果很好。问题是当PDF具有透明或半透明图形元素时,此代码会在添加图形时导致泄漏。
透明或半透明图形元素是指用透明或半透明颜色填充的矢量路径,而不是实心填充路径。或空白的帆布区域,不会造成泄漏问题。
- (id)initWithPDFResourceAtPath:(NSString *)path center:(CGPoint)center {
if ((self = [super init])){
CGPDFPageRelease(pageRef);
CGPDFDocumentRef documentRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path]);
pageRef = CGPDFDocumentGetPage(documentRef, 1);
CGPDFPageRetain(pageRef);
CGPDFDocumentRelease(documentRef);
[self setBounds];
}
return self;
}
-(void)setBounds {
[self setBounds:CGRectApplyAffineTransform(CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox), CGAffineTransformMakeScale(scaleH, scaleV))];
size = self.bounds.size;
[self getPDFimage];
}
-(void)getPDFimage {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, scaleH, scaleV);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextDrawPDFPage(context, pageRef);
[self setImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
}