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).
Related
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.
Before somebody says something about this topic being duplicated several times, please note that I really don't need loading remote images from the web (SDWebImage github solution), I'm trying to load images dynamically saved on the documents folder, so they are local by the time I want to load them on my tableView having reference to them via a record on a sqlite table in which I saved the image name as it was generated by code.
So after parsing all my sqlite data, in my CellForRowAtIndexPath method at a certain point I have the following lines of code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
NSString *imageName = [[favorites objectAtIndex:[indexPath row]] objectForKey:#"imagename"];
cell.image.image = [self loadImage: imageName]; // Is a custom cell so cell.image is a UIImageView, and I'm setting its image property
...
}
And my loadImage method looks like:
- (UIImage*)loadImage:(NSString*)imageName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]];
return [UIImage imageWithContentsOfFile:fullPath];
}
Note also that this code actually works but scrolling down the table looks laggy. How can I improve the performance, I mean loading images in the background comes to my mind but the images are already there. Can someone give me a concrete solution on this please?
Just to let you know, I'm working with iOS 5 and ARC.
Any help would be very appreciated.
Firstly decrease the size of image you load.
Secondly try to use GCD to load image in tableView:cellForRowAtIndexPath:.
I have Table View controller, then it has subclass DetailViewController, which content changes depending on cell chosen, but when move on, and from my DetailViewController go to MapView, I try to use same method I used to get text on DetailViewController, but it dont works, no matter what I do. Im stuck with it more like 3 week now:(
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
[self.navigationController pushViewController:self.detailViewController animated:YES];
[self.detailViewController changeProductText:[teksti objectAtIndex:indexPath.row]];
[self.detailViewController changeProductText1:[adrese objectAtIndex:indexPath.row]];
[self.detailViewController changeProductText2:[laimigastunda objectAtIndex:indexPath.row]];
[self.detailViewController changeImage:[imageChange objectAtIndex:indexPath.row]];
}
}
No matter what I change here, it dont work.
It looks from the code you are showing that you are not giving a data object to your detailViewController, but rather set directly the values.
This is not the way to do it and probably the reason why you are having issues. You need to grasp the concept of MVC and go back to it.
At least you should
1. build a dictionary in first view with keys #"teksti", #"adrese", #"longitude", #latitude".
2. Create a property in DetailViewController to hold the dictionary.
3. update the values displayed when displaying the DetailViewController
So that when you press the map button, you can then push a view containing a mapView and set the map to the latitude and longitude that you have.
So it would do:
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:[teksti objectAtIndex:indexPath.row], #"teksti", [adrese objectAtIndex:indexPath.row], #"adrese", [latitude objectAtIndex:indexPath.row], #"latitude", [longitudes objectAtIndex:indexPath.row], #"longitude", nil];
[detailViewController setDict:dict];
and in the DetailViewController.m:
- (void)viewWillAppear:(BOOL)animated{
[self changeProductText:[dict objectForKey:#"teksti"]];
.... And so on
}
Your code will be useful to provide you with more details directions.
I have a table view with several cells, each containing a UITextView.
The user can edit each text view that he clicks.
But when he clicks DONE, how do I access each text view, to read each that was edited?
You can use UItextField delegate to get the text from current text filed as -
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *textFieldValue = [textField text];
}
Let's say your UITextView is called "tv"
You can use something like:
NSString *contents = tv.text;
I believe you can also use something along the lines of:
NSString *contents = [tv getText];
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);
}