0

现在我正在使用这段代码。Pdf 正在生成,但图像未显示。

-(void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"%@",info);    
    //selectedImage =  [info objectForKey:UIImagePickerControllerOriginalImage];        
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];        
    NSLog(@" selected image is - %@",image);
    imageStr=[NSString stringWithFormat:@"%@",image]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *localFilePath=[documentsDirectory stringByAppendingPathComponent:@"images.pdf"];
    NSLog(@"file path is %@",localFilePath);
    [self pdfGenerate:localFilePath];
    UIGraphicsEndPDFContext();
    [imagePicker dismissViewControllerAnimated:YES completion:nil];
}

-(void)pdfGenerate:(NSString *)filepath
{
    UIGraphicsBeginPDFContextToFile(filepath, CGRectZero, nil);
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 300, 300), nil);
    [self convertImageToPdf];
}

-(void)convertImageToPdf
{
    CGRect imagerect=CGRectMake(0, 0, 300, 300);
    NSLog(@"image  is %@",imageStr);
    UIImage *image=[UIImage imageNamed:imageStr];
    [image drawInRect:imagerect];
}
4

1 回答 1

0

您将所选图像保存在 NSString 对象中imageStr (imageStr=[NSString stringWithFormat:@"%@",image];)

因此,当您在convertImageToPdf方法内创建图像 (UIImage *image=[UIImage imageNamed:imageStr];) 时,您的图像对象是nil. 所以pdf是空白的。

相反,您可以让 UIImage 引用您从选择器中选择的图像。使用里面的参考convertImageToPdf来绘制。

更新您的代码:

里面didFinishPickingMediaWithInfo

pickedImage=[info objectForKey:UIImagePickerControllerOriginalImage];        

里面convertImageToPdf

CGRect imagerect=CGRectMake(0, 0, 300, 300);
[pickedImage drawInRect:imagerect];

pickedImage是 UIImage 类型的对象。

于 2013-09-05T06:35:22.757 回答