take images from a cell , into next view controller - iphone

i asked this question yesterday but it doesnt seem to have asked properly so im trying again
in my app im parsing in an xml file , in that file there is a path for an image to download
an in my tableview controler i have this to download the image and display it in the cell
NSURL *url = [NSURL URLWithString:aStory.picture];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
im then pushing another view controller on the top and displaying some text in a text view , but i also want to take the image from the cell selected and put that in there too , but everything i try does not work.
any ideas on how i can do this
Thanks in advance
Richard

You need to create a property in the target ViewController to hold the image
UIImage *myDownloadedImage;
}
#property (nonatomic, assign) UIImage *myDownloadedImage;
and then when you are about to push you add the image to the controller
myNextViewController.myDownloadedImage = img;
[self.navigationController pushViewController:myNextViewController animated:YES];
Now you have access to the image to do as you wish

Related

Can't set UIImage to UIImageView from Url?

I can't understand why I can't set the uimageview from url from the example below:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://a4.mzstatic.com/us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"]];
UIImage *image = [UIImage imageWithData:data];
self.imgAlbum1 = [[UIImageView alloc] initWithImage:image];
it works fine by browsing the image, IBOutlet is ok, even setting uiimage from imagenamed doesn't work
I tried to recreate the contorl in the storyboard but nothing...
any tips are appreciated
Presumably imgAlbum1 is an outlet to your existing image view.
Instead of that last line, which tries to create a new image view without displaying it anywhere, you probably want:
self.imgAlbum1.image = image;
Edit: If your imgAlbum1 is an IBOutlet, you probably set it as a WEAK property. If that is the case, the image will never show up. In order to use alloc, your property would have to be a STRONG property.
Since you are probably using a WEAK outlet, try using self.imgAlbum1 as the other poster suggested. The reason this will work though, is because you're just updating the image, not the actual imageView object (weak properties, when you re-alloc will essentially destroy the object completely).
This is what I did which I tested and works just fine:
NSString *encodedString = [[NSString stringWithFormat:#"%#", #"us/r30/Music/08/e9/14/mzi.lmcnwrii.60x60-50.jpg"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://a4.mzstatic.com/%#", encodedString]];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageView *imgAlbum1 = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imgAlbum1];
So the questions for me are:
Are you using a WEAK property for imgAlbum1 (ie: #property (nonatomic, weak) IBOutlet *imgAlbum1)? If so, don't set it to strong - rather, just use self.imgAlbum1.image = image;
Have you ensured you have an internet connection with the device you are using?
Is imgAlbum1 an IBOutlet to a UIImageView ? If so, is it hidden (or hidden by a layer)?
Are you on the right view controller?
etc.
Otherwise, the code seems fine.

how to load few images from URL individually to scroll view ? objective-c

i have this scroll view, that I'm loading into him few images from URL.
the problem is that the scroll view don't show any of them until that all loaded.
i want to show every image the moment i finished loading her.
my code looks like this:
-(void)loadPhotosToLeftscroll{
for (int count = 0 ; count < [leftPhotoArray count]; count++) {
NSLog(#"nextPhotoHight: %f",nextLeftPhotoHight);
NSMutableDictionary *photoDict;
photoDict = [leftPhotoArray objectAtIndex:count];
float photoHight = [[photoDict objectForKey:#"photos_height"]floatValue];
float photoWide= [[photoDict objectForKey:#"photos_width"]floatValue];
NSString *photoPath = [photoDict objectForKey:#"photos_path"];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:photoPath]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(10 , nextLeftPhotoHight, photoWide, photoHight )];
[photoView setImage:image];
[photoView.layer setMasksToBounds:YES];
[photoView.layer setCornerRadius:6];
nextLeftPhotoHight = photoView.frame.size.height + photoView.frame.origin.y + 10;
[leftBlockScroll addSubview:photoView];
}
}
You better use a asynchronous way to do that.
Relative topic is NSURLConnection, UIImageView.
I have done something similar before.
1. Create a new model inherit to UIView
2. This model will have a UIImageView, NSData
3. When u init the model, pass in a URL
4. Use NSURLConnection to send out AsynchroizedRequest
5. By using NSURLConnection delegate, you will finally get the Data of the image
6. Init a UIImage with these data
7. Init The UIImageView with this Image
8. Add this imageview to this model or directly pointing this model to the imageview
Feel free to ask for more detail :)

iphone app - load cell image from XML element

I need some help to figure this out ..
I am getting data from the XML, one of the elements is the Image file Name "Icon1.png", images are save on my resources locally, at the moment I can load the icon1.png with this code
// retrieve an image
NSString *imageicon = [[NSBundle mainBundle]
pathForResource:#"icon2" ofType:#"png"];
UIImage *cellicon = [[UIImage alloc] initWithContentsOfFile:imageicon];
//set the image on the table cell
cell.imageView.image = cellicon;
but i want to load the icon file name from the XML element
<dimage>icon2.png</dimage>
how can I load the image to my cell ? - Solved
part 2 - selected row go to bclass description view controller,
-(void)viewDidLoad {
self.title = bclass.title;
[self.textView setText:bclass.description];
[self.destag setText:bclass.descimg];
}
at the moment "discing" is just showing the image name (UItextView), but i want to display the image on a UIImageView.
Thanks for your help.
Hernando.
Read the image file name from XML file to a NSString, and then create an UIImage using the
+(UIImage*)imageNamed:(NSString*)name class method of an UIImage object:
Example
NSString *imageName = ... read the name from xml.
cell.imageView.image = [UIImage imageNamed:imageName];

save a photo library URL as an NSString then back to URL

what i am trying to do is save a URL to one of the photo library pictures. then use this URL to set the image on a CCSprite.
this is the URL being saved as a NSString :
[_currentTarget setObject:[[NSString alloc]initWithString:[[info objectForKey:#"UIImagePickerControllerReferenceURL"]absoluteString]] forKey:#"image1Name"];
Then set the a property of a Target object i made
NSString *image1PathName = [[listOfUserMadeTargets objectAtIndex:num]objectForKey:#"image1Name"];
self.normalImage = image1PathName;
this is when the CCSprite is made
Target *target = [[Target alloc] targetThatIsUserMade];
[_targets addObject:target]; // add it to the array
target.position = ccp(spawnX,spawnY);
target.scale = -0.001875 * target.position.y + 1.09375;
[self addChild:target z:1];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:target.normalImage]]];
[target setTexture:[[CCTextureCache sharedTextureCache] addCGImage:image.CGImage forKey:#"key" ]];
The problem im having is that the Target is there, but the image is not
I have done this many different ways with no luck
thank you for any help
This is a post (and answer) I made which shows you how to save the NSURL for a UIImage in the CameraRoll, and use it later to get the image back, using a UIImagePickerController. This sample code should also help you in how to access the image. Hope that Helps!

xcode Caching images for images in UIScrollview

I have a list of images retrieve from .xml file and these images are images link from the server e.g. " www.seeimage.com/rice.png"
I am parsing the images everytime I went to that page
-(void)viewdidload{
for (int i = 0; i<[appDelegate.foodItems count];i++) {
NSURL *ZensaiimageSmallURL = [NSURL URLWithString:ZensaiPLUitems.ZensaiimageSmallURL];
NSString *string = [[NSString alloc] initWithFormat:#"%#", ZensaiimageSmallURL];
NSData *simageData = [NSData dataWithContentsOfURL:ZensaiimageSmallURL];
UIImage *itemSmallimage = [UIImage imageWithData:simageData];
[zenbutton2 setImage:itemSmallimage forState:UIControlStateNormal];
[scrollView addSubview:zenbutton2];
}
}
i have been trying out on this tutorial : http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/13315-image-caching-tutorial.html
but i have no idea on how to implement this in my work.
any idea on how to cache them on the first run and whenever i return to that view ?
i don't want to rerun this method to retrive the images from the website everytime i come to this view.
it takes quite some time to init the images from the website to my UIButton before populating them to the scrollview.
if you do not want to retrieve the images from the website every time then you can download it once and store them e.g. into NSMutableDictionary . And access it whenever you want.
OR
Parse those images in separate thread so that your table view or scroll view will not get paused.