problem with Image in custom Cell - iphone

Getting image data from sqlite database but not able to display in custom cell of imageview.is there any conversion of image data into image.

Super easy. There's a UIImage convenience method for exactly that.
//assuming the existence of an NSData *myData
UIImage *myImage = [UIImage imageWithData:myData];
So easy, in fact, I suspect that's not actually your question. If not, please clarify what you're asking.

Related

iOS : How to handle images?

I'm making a photo managing app. I have created a class called photo, which has the name of the photo, notes on the photo, and the image. I know how to set the name and notes - they're both NSStrings, but what data type should I use for my image?
At the moment, I'm using NSData, is that correct?
Edit
Apparently, NSData is right. So, how should I get an image from a UIImage into the NSData object and back again?
Data from UIImage:
NSData *imageData = UIImagePNGRepresentation(image);
Alternatively, UIImage from data:
UIImage *image = [UIImage imageWithData:imageData];
You can store your image as an UIImage or NSData
You can simply get one from another:
NSData *data = UIImagePNGRepresentation(image);
UIImage *image=[UIImage imageWithData:data];
In case you need the image for presenting it's better practice to hold an UIImage instance in youre object transferring it to/from NSData when you need to save it or get it either from the net or the disk. If youre case is more like file manager then photo viewer, then this answer is not aplicable. Anyway the transfering routines were provided to you by previous answer.

How to display image in the simulator?

I am using sqlite database and i have stored the path of image in my sqlite file.
But now I want to display image from the path stored in sqlite file.
for that I have written following code.
image_wine.image=[[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[wineDetail objectForKey:#"wine_photo1"]]];
where image_wine is my UIImageView and
wine_photo1 is my sqlite database field where path is stored.
but I am not able to get the image on my simulator when I run the app.
plz tell me how to solve the problem.
thanx.
You are doing a very basic mistake. You should assign UIImage instance to an UIImage object, not an UIImageView instance.
image_wine.image = [UIImage imageWithContentsOfFile:[wineDetail objectForKey:#"wine_photo1"] ];
image_wine.image=[[[UIImage alloc] initWithContentsOfFile:[wineDetail objectForKey:#"wine_photo1"]] autorelease];
or
mage_wine.image=[UIImage imageWithContentsOfFile:[wineDetail objectForKey:#"wine_photo1"]];
Into an UIImageView the propriety image is rapresented by an UIImage. Apple Doc
BTW [wineDetail objectForKey:#"wine_photo1"] is a path? If the image is contained in your project you should use imageNamed:

UIImage display differently when saving and loading from file

I'm download jpeg picture from web server and load it to UIImage.
If I display the UIImage in UIImageView directly, I see the picture correctly.
But if i cache the image to file with :
[UIImageJPEGRepresentation(image,1.0f) writeToFile:sFilePath atoimcally:YES]
and load it with :
image=[UIImage imageWithContentsOFile:sFilePath]
and display this in the same UIImageView, I can see white stripes in the sides of the picture.
again, Exactly the same UIImageView object with the same properties settings in it.
Why is that?
You can simply write to a file the NSData you have loaded from the web, without going through the UIImageJPEGRepresentation routine.
[dataObject writeToURL:fileURL atomically:YES];
and to retrieve
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[fileURL path]];
That works really well in my app.

UIImage in uitableViewcell slowdowns scrolling table

I am loading image with data in my table view. Images are coming from web. I created method to get image url in model class.Model class has Nsdictionary and objects.
But this images is slowing down scrolling .Here is my code
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",
[(Tweet *)[recentTweets objectAtIndex:indexPath.row]urlString]]]]];
cell.imageView.image = image;
Please tell Where I am going wrong?
Use lazy loading and do your own drawing. Try to understand the techniques on the sample projects I linked. These are the best ways to improve the performance of tables with images.
here is the methodology I use for loading images into a UITableView from a remote location:
in your .h file, declare a mutable dictionary for storing the images:
NSMutableDictionary *images;
initialize the dictionary in -init... or in -viewDidLoad
images = [[NSMutableDictionary alloc]init];
in the .m, tableView:cellForRowAtIndexPath:, see if the image exists in your dictionary for the indexPath
UIImage *img = [images objectForKey: indexPath];
if the image does exist, just set it.
if (img) cell.imageView.image = img;
if the image does NOT exist, set the cell's image to a temporary image...
if (!img) cell.imageView.image = [UIImage imageNamed:#"imageUnavailable.png"];
AND add that image to your dictionary so it doesnt try to refetch the image if you scroll off and back to that image before it loads...
[images setObject:[UIImage imageNamed:#"imageUnvailable.png"] forKey: indexPath];
then, in this same block, use an NSOperationQueue, and a custom NSOperation ( here is a reference - NSOperation and SetImage) to get your image and call back into your UITableViewController with that image.
in your callback, add your image to the dictionary (overwriting the temp image) and call [tableView reloadData]
this will give you a nice non blocking user experience.
The are a couple of ways how to do it. I had the best experience with a Queue for the HttpRequests, which I pause during the scrolling process.
I highly recommend this framework for that:
http://allseeing-i.com/ASIHTTPRequest/
I also implemented an image cache, which only loads the images if there weren't in the cache.
And the last tweak was to actually draw the images instead of using a high level uicomponent like the UIImageView
The number one reason your code is slow right now is because you're making a network call on the main thread. This blocks an UI from being updated and prevents the OS from handling events (such as taps).
As already suggested, I recommend ASIHTTPRequest. Combine asynchronous requests with an NSOperationQueue with a smaller concurrency count (say, 2) for the image requests, caching, and reloading the rows when images come in (on the main thread) (also only reloading the row if its currently visible). You can get a smooth scrolling table view.
I have an example of this on github here: https://github.com/subdigital/iphonedevcon-boston
It's in the Effective Network Programming project and includes a set of projects that progressively improve the experience.
Download the images before you load the tableView, and store them in an NSArray. Then when the cellForRowAtIndexPath method is called, show a loading image until the image is in the array.

UIImage UITableViewCell

How can I pass an image that is in my UITableViewCell to my TableDetailsView.
I tried setting the data on Post, which is my type of data that fills the UITableView cells.
[[cell avatar] setImage:[UIImage imageNamed:#"butterfly.jpeg"]];
[post setImage: [UIImage imageNamed:#"butterfly.jpeg"]]; <-- app crashes here.
All it seems that Question still is not complete....
But looking at two lines of code & you want to pass an image then you could do this thing in lots of ways.
so for testing purpose I will suggest ,just use a Global String i.e in Delegate.
like.Otherwise can you can use this link for Global Variable
Pass a variable between lots of UIVIewControllers
DelegateClassObj.yourGlobalString = [NSString stringwithFormat:#"%#",#"butterfly.jpeg"];
from the class where you want to pass.
&
now if you want to use it:
Suppose post in your imageView then
[post setImage: [UIImage imageNamed:DelegateClassObj.yourGlobalString]];
Hope this will work....the easir way :)
If Your motive is to Display same Image on detail view, then you can pass the name (or Id) of that image on cell tap. and load that image in your detail view through that.