簡體   English   中英

如何將選定的圖像從iPhone庫轉換為ios SDK中的pdf

[英]how to convert a selected image from iphone library to pdf in ios sdk

現在我正在使用此代碼。 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];
}

您正在將所選圖像保存在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類型的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM