I am a beginner trying to display an image, bitmap and tiff have been tried, in an image well or a button but with no success. The code is below. It runs but nothing is displayed. I have tried two ways of defining the string and also using self.well or _well but nothing works. If I try to use [well1 setImage:testImage]; as is shown in other examples I get an error "use of undeclared identifier.
//Code is
NSString *inFilePath = #"/Documents/Track1bmp.bmp";
NSImage *testImage = [[NSImage alloc] initWithContentsOfFile:inFilePath];
//well1 is your outlet
[self.well1 setImage:testImage];
[self.butt1 setImage:testImage];
[_well2 setImage:testImage];
// Also trying this
NSString* imageName = [[NSBundle mainBundle]
pathForResource:#"/Applications/Sudoku4/Pinktiff" ofType:#"TIFF"];
NSImage* tempImage = [[NSImage alloc] initWithContentsOfFile:imageName];
[self.well1 setImage:tempImage];
Related
I have the following code
NSString *myPath = [NSString stringWithFormat:#"../Documents/%#",#"bla.png"];
[imgView setImage:[UIImage imageNamed:myPath]]; // works fine
[self updateMyImage:[UIImage imageNamed:myPath]]; // loads another image as bla.png (works fine)
[imgView setImage:[UIImage imageNamed:myPath]]; // displays the old image, *not* the new one
The image is loaded in updateMyImage: (I open it (from the simulator's disk) and it is the "new" one)
but imgView displays the "old" one. I guess its a cache issue. Since the iOS has loaded the [UIImage imageNamed:#"bla.png"]
once, it thinks that this is the same one. How can I "flush" the cache and tell it to reload the image?
setImage: loads (as you corectly guess) from the cache.
Look for it in the 'UIImage Class Reference' (on xcode's Organizer window)
Few lines down it gives you the answer: use the initWithContentsOfFile: method.
So, after you change the image do this:
NSString *myAbsolutePath = [NSString stringWithFormat:#"%#/%#", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],#"bla.png"]];
UIImage *myImg = [[UIImage alloc] initWithContentsOfFile:myAbsolutePath];
[imgView setImage:myImg];
and now imgView will have the new image (as long as a img file exists at myAbsolutePath).
Check my app if you don't really understand ( Quick Notes!) But here it goes. My app is a notes app so it allows the user to select from few different kinds of note colors and designs below. When the user selects one, it changes the note above to what ever they set it to. So i need a button that will save the picture they selected, and when the leave the view and come back they can click the load button and the same image they selected will appear. I am using Xcode 4.3. Thanks so much!
Here is my save code:
-(IBAction)save123456 {
NSBitmapImageRep *rep;
NSData *data;
NSImage *noteview;
[self lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self frame]];
[self unlockFocus];
image = [[[NSImage alloc] initWithSize:[rep size]] autorelease];
[image addRepresentation:rep];
data = [rep representationUsingType: NSPNGFileType properties: nil];
//save as png but failed
[data writeToFile: #"asd.png" atomically: NO];
//save as pdf, succeeded but with flaw
data = [self dataWithPDFInsideRect:[self frame]];
[data writeToFile:#"asd.pdf" atomically:YES];
}
And my load:
-(IBAction)load123456 {
NSImage loadedImage = [[NSImage alloc] initWithContentsOfFile: NSString* filePath]
}
I get so many error codes in my save. My image is called noteview. Thanks!!
Try use instead
[data writeToFile: #"asd.png" atomically: NO];
a code line
[data writeToFile: #"asd.png" atomically: YES];
Some open source examples:
AGSimpleImageEditorView
image editors in iOS
projects with work with Pictures
I am creating an app that takes a json and parse it in order to obtain various NSDictionary filled by NSString.
When I start to show all these data inside my table view, Name and Address work fine, but I can't show up images, I actually use this brunch of code:
NSLog (#" URL img --> %#" , [dict objectForKey:#"urlimgp"]);
p.image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dict objectForKey:#"urlimgp"]]]];
NSLog (#" IMG --> %#" , p.image);
I can't figure out why the output from the first NSLog shows the exact url (so i know everything works fine with sql and php), but in the second NSLog I have got a (null). I really thank you in advance!
Your URL is not being encoded with percent escape encoding using UTF-8..
NSString *urlStr = [dict objectForKey:#"urlimgp"];
urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *data = [[[NSData alloc] initWithContentsOfURL:url] autorelease];
UIImage *image =[UIImage imageWithData:data];
When changing the image on an UIImageView in code, the image disappears. This only happens on an iPod Touch device, not on the iPhone emulator.
In my bundle I have an image named SomeImage.PNG
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeImage" ofType:#"png"];
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];
I must reiterate, this works fine on the iPhone simulator but not on an iPod Touch device.
Notice that the image file has an upper-case file extension, but in my code it is declared with lower case. This is the problem. So to fix the problem, change the code to this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"SomeImage" ofType:#"PNG"];
[self.background setImage:[[UIImage alloc] initWithContentsOfFile:path]];
I am asking if there is a 'better' way to do what I am doing, so I don't come across such issues in the future.
Not really. Unfortunately the iOS file system is case sensitive.
Something like this.....?
NSString *filename = #"SomeImage";
NSString *extension = #"png";
UIImage *img = nil;
NSString *path = nil;
path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension lowercaseString]];
img = [[UIImage alloc] initWithContentsOfFile:path];
if (img == nil) {
path = [[NSBundle mainBundle] pathForResource:filename ofType:[extension uppercaseString]];
img = [[UIImage alloc] initWithContentsOfFile:path];
}
I am having problem with PDF conversion to images. I would like to create an image file for every page in PDF document.
This is the code I am using and works fine. Every page gets converted into the image, but I have problem with image resolution. I don't know how to set the resolution of the output images. Can someone help me out?
NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://localhost/test/test.pdf"]];
NSPDFImageRep *img = [NSPDFImageRep imageRepWithData:pdfData];
NSFileManager *fileManager = [NSFileManager defaultManager];
int count = [img pageCount];
for(int i = 0 ; i < count ; i++) {
[img setCurrentPage:i];
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:#"Page_%d.jpg", [img currentPage]];
[fileManager createFileAtPath:[NSString stringWithFormat:#"%#/%#", #"/Users/mac/Desktop/", pageName] contents:finalData attributes:nil];
}
Thanks a lot!
Since NSPDFImageRep is a subclass of NSImageRep, couldn't you use the [NSImageRep drawInRect:] method?
Link: http://developer.apple.com/mac/library/documentation/cocoa/reference/ApplicationKit/Classes/NSImageRep_Class/Reference/Reference.html#//apple_ref/doc/uid/20000346-drawInRect_
The simplest way would be to use the ImageIO framework. Feed the PDF data to an image source to get a CGImage; feed that object to an image destination to generate (and optionally save in the same step) the JPEG data. In the latter step, you can specify the resolution among the image properties; see “Individual Image Properties” in the documentation.
Don't forget to finalize your destination. It's vital.