IOS opening PDF - ios

I am trying to open PDF file into my application with a custom UIBarButtonItem into the UINavigationBar, two options to open pdf into IOS
-UIDocumentInteractionController
-QLPreviewController
in IOS6 i have found this posts with prove that we cant customize the UINavigationBar
Custom navigationItem button with QLPreviewController in iOS6
QLPreviewController remove or add UIBarButtonItems
http://www.cimgf.com/2012/07/11/a-better-fullscreen-asset-viewer-with-quicklook/
Custom "Email" action in UIDocumentInteractionController
"iOS6 Update This technique of overriding the QLPreviewController will no longer work in iOS 6. I have contacted Apple about the situation, but they simply stated that it is no longer supported and it is considered a private API"
is there is any other way to open PDF in IOS without Using UIWebView and to customize the UIBarButtonItem.

I don't know what exactly you want to do with the PDF, but you can open a PDF file from the bundle using some code like this:
CGPDFDocumentRef pdfDoc;
CGPDFPageRef pdfPage;
NSURL* pdfURL = [[NSBundle mainBundle] URLForResource:#"filename" withExtension:#"pdf"];
CFURLRef urlRef = (__bridge CFURLRef)pdfURL;
pdfDoc = CGPDFDocumentCreateWithURL(urlRef);
if (pdfDoc == NULL) {
// Not good
}
if (CGPDFDocumentIsEncrypted (pdfDoc)) {
if (!CGPDFDocumentUnlockWithPassword (pdfDoc, "")) {
if (!CGPDFDocumentUnlockWithPassword (pdfDoc, "password")) {
// Not good, document is locked
}
}
}
if (!CGPDFDocumentIsUnlocked (pdfDoc)) {
CGPDFDocumentRelease(pdfDoc);
} else {
pdfPage = CGPDFDocumentGetPage(pdfDoc, 1);
CGPDFPageRetain(pdfPage);
// ...
}
From this point on the documentations from Apple should help you:
Quartz 2D Programming Guide
Core Graphics Framework Reference

Related

Create PDF with proper format and Images in Objective - C

Please read my scenario carefully,
I have one UITextView and one UIImageView bottom of TextView.
Each time there will be dynamic content in TextView and accordingly that, I am asking User to make a signature and it will be displayed as an image in bottom ImageView.
Now the requirement is I have to pass these details on the server along with Signature in one PDF file, So I have to create PDF file which contains both TextView text and ImageView image.
Note: TextView is containing Html text also, so it should show in the same format in PDF also.
Check below Images as required and current pdfs.
This is required PDF
This is current PDF
Only Put the code which can be helpful for both HTML support and Image merge with text. Please don't show simple PDF creation as I have done it already.
you don't need a 3rd party library, Cocoa and Cocoa touch have rich PDF support. I've stubbed you out a little start, do this in your viewController. There may be a few small errors, Ive been using swift for a couple of years now but I used my very rusty objC here because you tagged the question that way. Let me know any problems, good luck
-(NSData *)drawPDFdata{
// default pdf..
// 8.5 X 11 inch #72dpi
// = 612 x 792
CGRect rct = {{0.0 , 0.0 } , {612.0 , 792.0}}
NSMutableData *pdfData = [[NSMutableData alloc]init];
UIGraphicsBeginPDFContextToData(pdfData, rct, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();
//textView drawing
CGContextSaveGState(pdfContext);
CGContextConcatCTM(pdfContext, CGAffineTransformMakeTranslation(50.0,50.0));//this is just an offset for the textView drawing. You will want to play with the values, espeecially if supporting multiple screen sizes you might tranform the scale as well..
[textView.layer renderInContext:pdfContext]
CGContextRestoreGState(pdfContext);
//imageView drawing
CGContextSaveGState(pdfContext);
CGContextConcatCTM(pdfContext, CGAffineTransformMakeTranslation(50.0,50.0)); //this is just an offset for the imageView drawing. Thee same stuff applies as above..
[imageView.layer renderInContext:pdfContext]
CGContextRestoreGState(pdfContext);
//cleanup
UIGraphicsEndPDFContext();
return pdfData;
}
here's a couple of client functions to use this NSData
//ways to use the pdf Data
-(Bool)savePDFtoPath: (NSString *)path {
return [ [self drawPDFdata] writeToFile:path atomically:YES] ;
}
//requires Quartz framework.. (can be drawn straight to a UIView)
// note you MAY owe a CGPDFDocumentRelease() on the result of this function (sorry i've not used objC in a couple of years...)
-(CGPDFDocument *)createPDFdocument {
NSData *data = [self drawPDFdata];
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL , data , sizeof(data) ,NULL);
CGPDFDocument result = CGPDFDocumentCreateWithProvider(provider);
CGDataProviderRelease(provider); //not sure if this is still required under ARC?? (def not in swift)
return result;
}
Try this useful third party library :
https://github.com/iclems/iOS-htmltopdf
Use this function for your problem :
+ (id)createPDFWithHTML:(NSString*)HTML pathForPDF:(NSString*)PDFpath pageSize:(CGSize)pageSize margins:(UIEdgeInsets)pageMargins successBlock:(NDHTMLtoPDFCompletionBlock)successBlock errorBlock:(NDHTMLtoPDFCompletionBlock)errorBlock;

iMessage Apps - Programatically create animated sticker from .apng

Hi has anyone tried yet to programatically create a custom sticker programmatically with an animated apng file?
I've had no success but can state the following:
.apng file of correct dimensions and file size (300px * 300px) and < 500kb.
Can add this as a .png and it shows the first frame (not animated)
Changing file name to .apng causes an error to be raised upon inserting sticker to MS Message.
Have taken the approach of loading the apng into the project folder (not in Assets.xcassets) as the initialize sticker requires an NSURL which is not made availabe if apng is created as an iOS sticker.
Managed to user the same sequence in .GIF. this inserted correctly as a sticker.
Code below:
MSConversation *currentConversation = [self activeConversation];
NSURL *urlForSticker2 = [[NSBundle mainBundle] URLForResource:#"elephant_apng" withExtension:#"apng"];
MSSticker *challengeSticker2 = [[MSSticker alloc] initWithContentsOfFileURL:urlForSticker2 localizedDescription:#"My Sticker Localised" error:&myError];
[currentConversation insertSticker:challengeSticker2 completionHandler:^(NSError * error)
{
//HERE we get: error NSError * domain: #"com.apple.messages.stickers-error" - code: 8 0x0000600000058240
}];
So yes - keen to be able to overcome the error shown above.
Maybe you forget click here when you add the resource?
and your apng file's extension should be just "png" when you insert a MSSticker object.

Titanium Appcelerator - Remote URL not showing up as background image to View

So here's a nice one. I'm creating a imageView by doing this:
var tagView = Titanium.UI.createImageView({
backgroundImage: 'http://www.travelandtourworld.com/wp-content/uploads/2013/07/google-logo.jpg',
height:150,
width:365,
zIndex:10000
});
The problem is - anytime I use a remote URL as a background image it doesn't show up. Has anyone run into this and is there a good workaround for it?
It's just a rough guess but does it work when you use a normal View instead of an ImageView? Or try the image-property instead of backgroundImage-property for the ImageView. I just think that a background image is not the best practice to do on an ImageView even though the docs say it's possible.
I've done some testing with this as well and also found that backgroundImage doesn't work for remote URLs.
I've sort of fixed it by hacking this code into TiUtils.m of the Appcelerator core (tested with 3.5.0.GA).
if (resultImage == nil) {
if ([image isKindOfClass:[NSString class]]) {
NSURL* imageURL = [TiUtils toURL:image relativeToURL:nil];
resultImage = [[ImageLoader sharedLoader] loadRemote:imageURL];
}
}

Screenshot Safari from Share Extension

Is it possible to perform a screenshot of the current visible zone of the webview in Safari from a Share Extension? I could use windows, but UIApplication isn't supported on extensions so I can't access to that window.
You can't since UIApplication can't be reached from an extension. You cannot get the first UIWindow, which is the Safari layer, so you have to play with the Javascript preprocessing file that the extensions have. So just create a Javascript file that, when sent to Safari, generates a base64 string with the current visible zone image data. Take that string through the kUTTypePropertyList identifier in your extension. Since that should be NSData, generate the UIImage from there, by using +imageWithData. That is what you're looking for, without having to load the page again, preventing a second load and a bad image if the webpage requires of a login.
As far as I know, you can't unless you invoke the API you need dynamically, and even so you might run into context permission issues and app store approval issues.
An alternative might be passing the current Safari URL to your extension, load it using a hidden UIWebView and render this view into an UIImage but you will loose the current visible zone information...
Edit: So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.
You can't get just the visible area of Safari, but you can get a screenshot with a little ingenuity. The following method captures a screenshot from a ShareViewController.
func captureScreen() -> UIImage
{
// Get the "screenshot" view.
let view = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
// Add the screenshot view as a subview of the ShareViewController's view.
self.view.addSubview(view);
// Now screenshot *this* view.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Finally, remove the subview.
view.removeFromSuperview()
return image
}
This is the approved way to capture the screenshot of a webpage in a share extension:
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
[itemProvider loadPreviewImageWithOptions:#{NSItemProviderPreferredImageSizeKey: [NSValue valueWithCGSize:CGSizeMake(60.0f, 60.0f)]} completionHandler:^(UIImage * item, NSError * _Null_unspecified error) {
// Set the size to that desired, however,
// Note that the image 'item' returns will not necessarily by the size that you requested, so code should handle that case.
// Use the UIImage however you wish here.
}];
}
}

How to use "pdfView" in object library in iOS?

I see in "object library" pdfView is there, but, when I click on .XIB it got disappers.How can I use this for iPhone or IPad development? Plaese provide me any example links.Thanks.
Note: Plaese do not tell me to use any 3rd party lib,because I'm already refering two.This i don't want ot use going forword.
PDFView is only for cocoa(OSX) not for iOS.
A PDFView object encapsulates the functionality of PDF Kit into a single widget that you can add to your application
PDFKit only available in OSX.
In iOS to display pdf pages you don't want use any third party libraries, you can draw the pdf view by creating a custom view override the draw something like
Get the pdf document
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:#"aa" ofType:#"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
currentPage = 1;
In drawRect
-(void)drawRect:(CGRect)rect
{
CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true));
CGContextDrawPDFPage(ctx, page);
CGContextRestoreGState(ctx);
}
Are you posting two questions that are pretty much the same?
A couple of minutes ago you posted this: Object library not visible when click on .XIB in xcode ios
My answer still stands, you have chosen a OSX xib NOT an iOS xib.
If you want to show a PDF in an iOS app, you could add it to a UIWebView. Have a look at this question for details: iPhone : can we open pdf file using UIWebView?

Resources