I'm sending a document between the desktop and a handheld device, and I want to add a metadata header to a PDF like the following.
<CUSTOM_HEADER>\n
{"fileInfoEncodedInJson\":
{"filename":"My Print Out",
"filesize\":"630",
"filedesc":"",}
}\n
</CUSTOM_HEADER>\n
… file contents …
I've been using the PDFKit and PDFDocument which supplies the documentAttributes and setDocumentAttributes methods, but because it is a custom header, it doesn't seem to persist when I set the attributes and save the file:
NSURL *path = [NSURL fileURLWithPath:#"/Users/username/Desktop/file.pdf"];
PDFDocument *document = [[PDFDocument alloc] initWithURL:path];
NSDictionary *docAttributes = [self.document documentAttributes];
NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc] initWithDictionary:docAttributes];
[newAttributes setObject: #"Custom header contents" forKey:#"Custom header"];
docAttributes = [[NSDictionary alloc] initWithDictionary: newAttributes];
[document setDocumentAttributes: docAttributes];
//Here Custom Header is an entry in the dictionary
[self.document writeToFile:#"/Users/username/Desktop/UPDATEDfile.pdf"];
//Here the UPDATEDfile.pdf does not contain the custom header
I've been looking all over and I've found a couple of similar questions (like here on cocoadev for example) but no answers. Does anyone know of a way to store custom (i.e. not the 8 predefined constants provided under Document Attribute Keys) headers to PDF Files?
I didn't actually edit the pre-existing headers, I simply created a NSMutableData object, added the text data first followed by the PDF data, then saved this data to the path I wanted.
NSString *header = [NSString stringWithFormat:#"<CUSTOM_HEADER>\n {\"fileInfoEncodedInJson\": {\"filename\":\"My Print Out\", \"filesize\":\"630\",\"filedesc\":\"\",} }\n </CUSTOM_HEADER>\n"];
// Append header to PDF data
NSMutableData *data = [NSMutableData dataWithData:[header dataUsingEncoding:NSWindowsCP1252StringEncoding]];
[data appendData:[doc dataRepresentation]];
[data writeToFile:#"/Users/username/Desktop/UPDATEDfile.pdf" atomically:NO];
This results in a PDF file that opens in Adobe for me, and the header is invisible when viewing.
Related
My viewController has the following method, which gets called after an image gets selected via a UIImagePickerController. I'd like to upload the selected image to my web service, however, when attempting to follow the samples provided by RestKit I get the following error:
No visible #interface for 'RKObjectManager' declares the selector 'multipartFormRequestForObject:method:path:parameters:constructingBodyWithBlock:'
I'm using the most recent version of restkit, and right clicked went to definition to check the signature which seems correct.
It is worth noting that AFMultipartFormData is not highlighting in XCode. I tried including #import AFNetworking/AFHTTPClient.h but it still shows as plain text, which i suspect might be the problem?
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
ImageRecord *imageRecord = [ImageRecord new];
NSDictionary *params = #{#"param1" : #"value1",
#"param2" : #"value2",
#"param3" : #"value3"};
// Serialize the Article attributes then attach a file
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestForObject:imageRecord method:RKRequestMethodPOST path:#"stuff" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(image)
name:#"article[image]"
fileName:#"photo.png"
mimeType:#"image/png"];
}];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:nil failure:nil];
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation]; // NOTE: Must be enqueued rather than started
[self dismissViewControllerAnimated:YES completion:NULL];
}
Thanks for any pointers!
That method is multipartFormRequestWithObject: (note that the method name you're using has ForObject). You shouldn't need to import any additional headers.
I'm using magical record with Core Data.
In my app I have just one entity with some string attributes. Now, I would like to add an image to this entity, but I don't have any idea of how to do that using magical record. I searched but haven't found anything on the web. In my app all the data is inserted by the user, so also the image, by the camera or the photo library.
How do I store images using Magical Record and Core Data?
Just store the image in the documents folder of the app and save a string with the file url in the core data entity.
What you're trying above can be done easily. Best advice i can give you is make a new sample project just to save and retrieve image from the CoreData database. That way you know exactly how the process works. If you try to embed this functionality in your current project you might loose track of what happening where.
This is very quick example, i'll expect you to import the Delegates in your .h files yourself etc.
First initiate the UIImagePicker via button
-(IBAction)pickImage:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
}
Once the image is selected, you can display it on the button using one of the delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
self.imageButton.imageView.image = selectedImage;
To save it to CoreData assign your UIImage type to a Transformable type in your attribute in your database and then save the managedObjectContext
Links to help you:
UIImagePicker Class Reference
Magical Record Docs
CoreData Recipes Sample Project
Hope this help, good luck!
I found my answer:
to save:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString*nomeImmagine = [[NSString alloc] initWithFormat:#"%#", self.fieldName.text];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#.png",documentsDirectory, nomeImmagine];
UIImage *image = self.showSelectedImage.image; // imageView is my image from camera
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data1 writeToFile:pngFilePath atomically:NO];
to load:
-(void) loadImageFromPathInsideView
{
// [self loadImageFromPathInsideView];
Ricetta* contact =[[DataManager sharedClass]dammiTuttaLaRicetta:self.indice];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString*nomeImmagine = [[NSString alloc] initWithFormat:#"%#", contact.nome2];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [NSString stringWithFormat:#"%#/%#.png",documentsDirectory, nomeImmagine];;
UIImage* image = [UIImage imageWithContentsOfFile:path];
self.image.image = image;
}
a word of advice. storing images as transformable (aka NSImage) type in core data is easy, but leads to overall slow app performance for even a low number (over 20) that range from 50k to 200kb with an average of 100k. even having those files linked via a relationship is slow, considering that one controller is often bound to another.
the above method of storing a local path name as a NSString pointing to Documents folder is heaps better for the overall experience.
that being said, creating a thumbnail image (of around 150x150) can be advantageous to create once and store that as a transient transformable NSImage, rather than loading the biggy and doing an on-the-fly resize a number of times. that performance can be observed easily scrolling up and down in a Table holding 50+ thumbnail images.
I try to make a PDF document manager for my own OSX by Xcode. Now I can get the URL of the document. With the code below:
NSOpenPanel *documentOpenPannel = [NSOpenPanel openPanel];
NSInteger situationInt = [documentOpenPannel runModal];
if (situationInt == NSOKButton) {
NSURL *documentPath = [[documentOpenPannel URLs] lastObject];
}
From the documentPath, I can get some properties already, like file size:
NSString *fileSize;
[documentPath getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil];
Now I want to get more attributes of the documents, e.g. version number, total pages.
Could you give me some suggestion?
You need to use CGPDFDocument. CGPDFDocumentGetVersion(), CGPDFDocumentGetNumberOfPages() solve your specific problem.
I'm sending an image name from a view controller. In the recieving view controller I want to set the image in UIImageview to the string plus .png
The code I'm trying (which doesn't work) is:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *imagefile;
imagefile=[NSString stringWithFormat:#"%d.png",myColour];
myImage.image=[UIImage imageNamed:imagefile];
[output setText:myString];
// [imagefile release];
}
In the code here myColour is the string passed from the previous page - so I have images called yellow.png, blue.png etc
Thanks.
The code you want is:
imagefile = [NSString stringWithFormat:#"%#.png", myColour];
Use %# to print values from objects (NSObject subclasses, like NSString).
I capture a image of webview that playing a flash. Because I want to show this image and use the IKSaveOptions to save the image .but I found the path of image is nil, Now I want to get the path of image , How to do ?
my code:
NSString *path = [[NSBundle mainBundle] pathForResource: ?? ofType:??];
NSURL *url = [NSURL fileURLWithPath: path];
then I use the url to show the image in the imageview,but Now I didn't get the path ,Thanks a lot!
thanks a lot. Now I think that the NSBundle is not my option. But I want to show the image of capturing it from a webview showing a flash. I can show it in a imagecell, but show in the imageview ,I use the code:
[imageView setImage:image imageProperties: mImageProperties];
and the mImageProperties come from ahead of the document of you take me the ImageKit documentation, the name is "Viewing an Image in an Image View" ,and in there the mImageProperties is the properties of image . code is :
mImageProperties = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(isr, 0, (CFDictionaryRef)mImageProperties);
still ues the url of the image, if I couldn't use the NsBundle , How I can do to get the properties of the image, by the way, I have already get the image of the capturing webview of showing a flash. code :
NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView frame]];
[webView cacheDisplayInRect:[webView frame] toBitmapImageRep:imageRep];
NSImage *image = [[NSImage alloc] initWithSize:[webView frame].size];
[image addRepresentation:imageRep];
Okay, you are confusing a lot of things here. First off, IKSaveOptions does not save an imnage. It is a mechanism for presenting an interface to the user about the options the want for saving, but it does not actually save the file anywhere. To save the file you use the underlying CGImage mechanisms, as described in the ImnageKit documentation. I think if you read the example code there it will also be clear where the path to the saved file is.
Now, onto the second issue. You would never use pathForResource:ofType to get it. That gets resources that are in your application bundle. In other words, things that are a part of your application, that you include with it at build time. You should NEVER modify your bundle contents after build, aside from being complicated, it will invalidate codesigned applications. Instead you should probably use either CGImage or NSImage to read it in.
ok, I have the answer.
NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:tmpRect];
[webView cacheDisplayInRect:tmpRect toBitmapImageRep:imageRep];
NSImage *theimage = [[NSImage alloc] initWithSize:tmpRect.size];
[theimage addRepresentation:imageRep];
CGImageSourceRef isr = CGImageSourceCreateWithData((CFDataRef)[theimage TIFFRepresentation], NULL);
CGImageRef image = NULL;
if (isr)
{
image = CGImageSourceCreateImageAtIndex(isr, 0, NULL);
if (image)
{
mImageProperties = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(isr, 0, (CFDictionaryRef)mImageProperties);
}
CFRelease(isr);
}
if (image)
{
[imageView setImage:image imageProperties:mImageProperties];
CGImageRelease(image);
}