How to add a jpg or png image to an UIImage and then display in a UIImageView? - image

UIImage *img = [[UIImage alloc] initWithContentsOfFile:#"contactHeader.png"];
_headersView = [[UIImageView alloc] initWithImage:img];
I have already hooked up the connections for the UIImageView (_headersView) in IB. The image I want to load is in my project tree structure as well.
Am I creating the UIImage the right way?

If the file is in your bundle you can just use
UIImage *img = [UIImage imageNamed:#"contactHeader"];
_headersView = [[UIImageView alloc] initWithImage:img];
If the image is in your documents directory you can use
// Get the path to your documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Add the file to the end of the documents path
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"contactHeader.png"];
UIImage *img = [[UIImage alloc] imageWithContentsOfFile:filePath];
_headersView = [[UIImageView alloc] initWithImage:img];
[img release]; img = nil;

try this
-(NSString *)getPath:(NSString *)filename
{
self.path = [[NSBundle mainBundle]pathForResource:filename ofType:#"png"];
return path;
path = nil;
}
self.youImageView.image = [UIImage imageWithContentsOfFile:[self getPath:#"123"]];

Related

iOS UIImageView animationImages display remote pictures

I have three urls just like "http://xxx.x.xx.xx:8180/admin/upload/advert/advert_img.jpg",
they are pictures in the remote server.
And I have an UIImageView, I want to use:
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 504.0, 320.0, 40.0)];
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"advert_img"],[UIImage imageNamed:#"img01"],[UIImage imageNamed:#"img02"], nil];
_imageView.animationImages = images;
_imageView.animationDuration = 15;
[_imageView startAnimating];
to display these pictures. How can I do this? Thank you very much.
Try this :
NSArray *frames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"advert_img.png"],
[UIImage imageWithName:#"img01.png"],
[UIImage imageWithName:#"img02.png"],
nil];
UIImageView *animatedIMvw = [[UIImageView alloc] init];
animatedIMvw.animationImages = frames;
[animatedIMvw startAnimating];

How to take a screenshot when a webview finished rending

I want to take a screenshot when a webview finished rending.The follow is my code:
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if (frame != [sender mainFrame]) {
return;
}
NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:[sender bounds]] autorelease];
if (rep){
NSImage *img = [[NSImage alloc] initWithData:[rep TIFFRepresentation]];
NSData* imgData = [img TIFFRepresentation];
NSArray* deskTopArrayPaths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString* deskTopPath = [deskTopArrayPaths objectAtIndex:0];
NSString* pngPath = [NSString stringWithFormat:#"%#/SaveWebPage.png",deskTopPath];
[[NSFileManager defaultManager] createFileAtPath:pngPath contents:imgData attributes:nil];
}
}
The normal image is that:
But my image is this:
Anyone tell me why and how could i improve this situation! Thank u!

saving an image to documents directory - not working - only creating a zero byte file

I am trying to make a simple app to take a picture and save the file to both a UIImageview and to the documents folder so it can be recalled at a later time as the UIImageview. To get visibility into if the app is actually making a file, I have turned on Application supports iTunes File sharing within the .plist file.
Problem is the file generated is zero bytes. I have tried both PNG and JPG format.
- (IBAction)picImage:(id)sender {
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//check to see if the device has camera capability
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else
{
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//set the UIImageView to the selected image
playerImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"player.png"];
UIImage *editedImage = [UIImage imageWithContentsOfFile:imagePath];
NSData *imageData = UIImagePNGRepresentation(editedImage);
//NSData *imageData = UIImageJPEGRepresentation(editedImage,1.0);
// This actually creates the image at the file path specified, with the image's NSData.
[[NSFileManager defaultManager] createFileAtPath:imagePath contents:imageData attributes:nil];
//dismiss the imagepicker view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Try this code and let me know is it working or not..!!!!
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *strimagename;
strimagename=[NSString stringWithFormat:#"Test.jpg"];
NSString *thumbFilePath = [documentsDirectory stringByAppendingPathComponent:strimagename];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *thumbData = UIImageJPEGRepresentation(image, 1);
[thumbData writeToFile:thumbFilePath atomically:YES];
[imgPicker dismissModalViewControllerAnimated:YES];
This code will save image in documentory folder..
Happy Coding..!!!!!!

How do I convert an annotated PDF to an image?

I have an annotated PDF and want to convert it to image. The pdf is successfully getting converted to an image but annotations are missing in the conversion.
This is the code:
NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfFile:PdfFilePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
[img setCurrentPage:0];
NSImage *temp = [[NSImage alloc] init];
[temp addRepresentation:img];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
NSString *pageName = [NSString stringWithFormat:#"Page1_%d.jpeg", 0];
NSString *imgFilePath = [NSString stringWithFormat:#"%#/%#",#"/Users/Desktop/PDFImages", pageName];
[fileManager createFileAtPath:imgFilePath contents:finalData attributes:nil];
Is there a different way to store the annotated pdf to image?
The issue is resolved using image lock and unlock feature.
NSSize pageBounds1;
pageBounds1 = [page1 boundsForBox: [_pdfView displayBox]].size;
NSImage *temp = [[NSImage alloc] initWithSize:pageBounds1];
[temp lockFocus];
[page1 drawWithBox:kPDFDisplayBoxMediaBox];
[temp unlockFocus];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
NSString *pageName = [NSString stringWithFormat:#"Page1_%d.jpg", pageIndex];
NSString *imgFilePath = [NSString stringWithFormat:#"%#/%#",pathForImages, pageName];
[fileManager createFileAtPath:imgFilePath contents:finalData attributes:nil];

Trying to save/load image in app error NSInvalidArgumentException

Im trying to save and load an image picked from camera roll within my app. I get an error when I press either the load or save button
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView CGImage]: unrecognized selector sent to instance 0xe637640'
any thoughts?
AppDelagate .m
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory i nDomains:NSUserDomainMask] lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
.m of class im trying to save/load image
- (IBAction)save:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
}
- (IBAction)load:(id)sender {
UIImage *myImage = imageView;
NSData *data = UIImagePNGRepresentation(myImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}
this error means that message CGImage is sent to object of type UIImageView, but UIImageView does not response to it. The problem is seemed to be in
UIImage *myImage = imageView;
You do not write properly implementation of imageView, so I can not say exactly, but I assume that imageView is UIImageView, which is subclass of UIView, and you are assigning it to UIImage. Try
UIImage *myImage = [imageView image];
instead. I think that should works))
by the way, why do you need first and second strings of code in -load? They are unnecessary:
- (IBAction)load:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *appDocsDirectory = [paths objectAtIndex:0];
UIImage* thumImage = [UIImage imageWithContentsOfFile: [NSString stringWithFormat:#"%#/%#.png", appDocsDirectory, #"myNewFile"]];
}

Resources