J.Chandrasekhar Reddy
J.Chandrasekhar Reddy

Reputation: 101

Converting multiple UIImages stored in NSMutableArray to PDF

I am converting each page of a PDF file into UIImage and store it in NSMutableArray.Now I want to convert the same UIImages into the PDF file as before. I am able to convert the single image but i want all the images to be converted at a time and should be same as previous PDF file.

My code for converting UIImage into PDF page:

UIImage *image1 = [ UIImage originalSizeImageWithPDFNamed:[NSString stringWithFormat:@"%@%@",fileName,@".pdf"] atPage:i+1 ];
imageData = [NSData dataWithData:UIImagePNGRepresentation(image1)];
[thumbImage addObject:[UIImage imageWithData:imageData]];

How to convert entire UIImages into a single PDF file?

Upvotes: 0

Views: 1051

Answers (1)

Refael.S
Refael.S

Reputation: 1644

Use this method:

- (void)createPDFWithImagesArray:(NSMutableArray *)array andFileName:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *PDFPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",fileName]];

    UIGraphicsBeginPDFContextToFile(PDFPath, CGRectZero, nil);
    for (UIImage *image in array)
    {
        // Mark the beginning of a new page.
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, image.size.width, image.size.height), nil);

        [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    }
    UIGraphicsEndPDFContext();
}

Upvotes: 4

Related Questions