I am not getting my image working. Can anyone tell me what I am doing wrong here?
musclePicture is a string in format of picture.png
1st view:
NSString *muscleURL = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:#"musclePicture"];
specificExerciseTableViewController.muscleImage = [UIImage imageNamed:muscleURL];
2nd view:
detailViewController.muscleImage = self.muscleImage;
3rd view:
self.image = [[UIImageView alloc]initWithImage:muscleImage];
[image release];
Edit:
MusclesTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *muscleURL = [[self.muscleArray objectAtIndex:indexPath.row]objectForKey:#"musclePicture"];
specificExerciseTableViewController.muscleImage = [UIImage imageNamed:muscleURL];
}
SpecificExerciseTableViewController.h
#property (nonatomic, retain) UIImage *muscleImage;
SpecificExerciseTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
detailViewController.muscleImage = self.muscleImage;
}
DetailViewController.h
#property (nonatomic, retain) UIImage *muscleImage;
#property (nonatomic, retain) IBOutlet UIImageView *image;
DetailViewController.m
- (void)viewDidLoad
{
self.image.image = muscleImage;
}
Try by giving a frame to your UIImageView after:
self.image = [[UIImageView alloc]initWithImage:muscleImage];
[image setFrame:CGRectMake(0,0,50,50)];
[image release];
Try this code snippet:
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[animal imageURL]]];
UIImage *animalImage = [[UIImage alloc] initWithData:imageData];
Assign it your UIImageView or UIImage.
I think you forgot to add image as subView to your view.
like this
[self.view addSubView:image];
or you are having a class variable that is connected to .xib file as an IBOutlet and you are reinitializing it using alloc and init, if that is the case then you have to do
self.image.image = [UIImage imageNamed: muscleImage];
Related
I am new on objective c and I am trying to set image of UIImageview while pushing viewcontroller
I checked other answer and I did the same way they said it but its not working for me below is my code and i will explain other things in it
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *svController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
UIImage *image = [UIImage imageNamed:#"default.png"];
UIImageView* imageview = [[UIImageView alloc] initWithImage:image];
id obj = [[mainArray valueForKey:#"image"] objectAtIndex:[indexPath row]];
if ([obj isKindOfClass:[NSString class]])
{
NSLog(#" image%#",image); //print this image<UIImage: 0xaa70700>
NSLog(#" image%#",svController.image); //print this image(null)
[[svController image] setImage: image];
NSLog(#" image%#",svController.image); //print this image(null)
}
else {
NSLog(#" image%#",svController.image);
[[svController image] setImage: [[mainArray valueForKey:#"image"] objectAtIndex:[indexPath row]]];
NSLog(#" image%#",svController.image);
}
[self.navigationController pushViewController:svController animated:YES];
[svController release];
svController = nil;
}
below is my detailviewcontroller
#interface DetailViewController : UIViewController{
UIImageView *image;
}
#property (retain, nonatomic) IBOutlet UIImageView *image;
its connected to Xib and i have synthesized it
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
name.text = nameString;
email.text = emailString;
NSLog(#"%#",image); // print this <UIImageView: 0x9d73950; frame = (27 35; 204 181); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x9db4050>>
}
its working perfectly when i send NSString and set them on my lable but not working for image it its so small thing but now i dont know what to do please help
hear is the simple way, i am showing the example that u can set image in detail view controller
in your master view controller
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)//for 1st row
{
DetailViewController *detController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detController.detailImage = [UIImage imageNamed:#"abc.png"];//setting the property for image
[self.navigationController pushViewController:detController animated:YES];
[detController release];//im doing without ARC
}
else if (indexPath.row == 1)
{
DetailViewController *detController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detController.detailImage = [UIImage imageNamed:#"123.png"];
[self.navigationController pushViewController:detController animated:YES];
[detController release];//im doing without ARC
}
}
and in your detail view controller
// .h file
#interface DetailViewController : UIViewController
{
}
#property (retain, nonatomic) IBOutlet UIImageView *ImageView;
#property (nonatomic, retain)UIImage * detailImage;//set a property for image that u are passing from master viewcontroller
#end
//in .m file
#implementation DetailViewController
#synthesize detailImage; //synthesise it
// and in viewDidload method
- (void)viewDidLoad
{
[super viewDidLoad];
//set your passed image to image view
self.ImageView.image = self.detailImage;
}
Better would be just do like this:
[self.navigationController pushViewController:svController animated:YES];
if(yourImage) //got UIImage reference then only set it
svController.yourImgView.image = yourImageHere
.................
<<Another code here>>
..............
Thats it....
You can't do this, because svController's view has not been loaded at the time you try to set the image of its image view (so the image view will be null).
You need to pass the image itself to a property you create in svController, and set the image in svController's viewDidLoad method.
Set your UI object intialization afer pushing view controller.
[self.navigationController pushViewController:svController animated:YES];
//set image here
Reason is viewDidLoad of svController will not be called and loaded yet and imageview will not have reference yet.
EDIT :
Better would be just do like this:
[self.navigationController pushViewController:svController animated:YES];
if(yourImage) //got UIImage reference then only set it
svController.yourImgView.image = yourImageHere
.................
<<Another code here>>
..............
Thats it....
In this code a user gets and displays a photo (provided for now) in ImageViewController, adds a caption, and the the photo and caption are passed to the MTViewcontroller where it is made into a pdf. I have solved the PDF portion, but struggling with the segue. Appreciate any guidance. I can't find an example quite like this.
ImageViewController.h
#import <UIKit/UIKit.h>
#interface ImageViewController : UIViewController
{
NSURL *url;
UIImage *imageRoll;
}
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) IBOutlet UITextField *enterCaption;
- (IBAction)Button:(id)sender;
#end
ImageViewController.m Not sure I'm treating imageRoll correctly and I can't find the right segue format for this. Once the user, submits the caption it would segue to the PDF controller. BUT, would the keyboard release given the segue?
- (void)viewDidLoad
{
[super viewDidLoad];
// Since iPhone simulator doesn't have photos, load and display a placeholder image
NSString *fPath = [[NSBundle mainBundle] pathForResource:#"IMG_3997" ofType:#"jpg"];
url = [NSURL fileURLWithPath:fPath];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
UIImage *imageRoll = [UIImage imageWithContentsOfFile:fPath];
}
// User provides a caption for the image
- (IBAction)Button:(id)sender {
NSString *caption = enterCaption.text;
[self performSegueWithIdentifier:#"showPDF" sender:sender];
MTViewController *vc1 = [segue destinationViewController];
vc1.photoCaption = caption;
MTViewController *vc2 = [segue destinationViewController];
vc2.photoImage = imageRoll;
}
//Dismiss Keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
MTViewController.h
#import <UIKit/UIKit.h>
#import "ReaderViewController.h"
#interface MTViewController : UIViewController <ReaderViewControllerDelegate>
#property(nonatomic, assign) NSString *photoCaption;
#property(nonatomic, assign) UIImage *photoImage;
- (IBAction)didClickMakePDF:(id)sender;
#end
MTViewController.m Have I passed and used photoCaption and photoImage correctly?
- (IBAction)didClickMakePDF {
[self setupPDFDocumentNamed:#"NewPDF" Width:850 Height:1100];
[self beginPDFPage];
CGRect textRect = [self addText:photoCaption
withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];
CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4)
withColor:[UIColor blueColor]];
UIImage *anImage = [UIImage imageNamed:#"photoImage"];
CGRect imageRect = [self addImage:anImage
atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)];
[self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4)
withColor:[UIColor redColor]];
[self finishPDF];
}
updated:
You should use self.xxx = xxx to save it in one method and get it back in another method by using self.xxx. For example you should use
self.imageRoll = [UIImage imageWithContentsOfFile:fPath];
instead of declaring a new imageRoll in your code. And it's the same with your caption.
The way you use segue is incorrect.
The way you call [self performSegueWithIdentifier:#"showPDF" sender:sender]; is correct. But then you can do some customization in
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showPDF"]) {
MTViewController *vc1 = [segue destinationViewController];
vc1.photoCaption = self.caption;
vc1.photoImage = self.imageRoll;
}
}
I've written some code to make a screenshot of the view. I write that image to the photo library. But the thing is, I want to use that image in an other imageView in another ViewController. How can I save the image somewhere in the app and use it in another ViewController?
My code:
UIView* captureView = self.view;
UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0);
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect cropRect = CGRectMake(0 ,0 ,640,1136);
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.0f);
[screenshot drawInRect:cropRect];
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil);
You first need to save your image on the disk:
NSString *documentDirectory =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/myImage.png",documentDirectory];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(customScreenShot)];
[imageData writeToFile:pngFilePath atomically:YES];
And then you can create an UIImageView with the file:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage
imageWithContentsOfFile:pngFilePath]];
[self.view addSubview:myImageView];
If you are using Storyboards, you could pass it to the next view controller like so; in the prepareForSegue method ( this relieves you from having to save it to disk ):
Note: the name of the segue is set by you in MainStoryboard.storyboard. - mySegue.
kMySegue is just a key for that. i.e. #define kMySegue #"mySegue"
imageInOtherViewController is a UIImage in the other view controller.
// TheFirstViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
id destinationViewController = [segue destinationViewController];
if ([[segue identifier] isEqualToString:kMySegue]){
if ([destinationViewController respondsToSelector:#selector(setImageInOtherViewController:)]){
[destinationViewController setImageInOtherViewController:[UIImage imageNamed:#"myImage.png"]];
FastCameraViewController *viewController = segue.destinationViewController;
viewController.delegate = self;
}
}
// OtherViewController.h
#interface OtherViewController : UIViewController
{
}
#property (nonatomic, strong) IBOutlet UIImageView *otherViewControllerImageView;
#property (nonatomic, strong) UIImage *imageInOtherViewController;
// OtherViewController.m
#implementation OtherViewController
#synthesize otherViewControllerImageView;
#synthesize imageInOtherViewController;
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self otherViewControllerImageView] setImage:imageInOtherViewController];
}
To cache your images I recommend using the NSCache class first ie
NSCache* imagesCache = [[NSCache alloc] init];
[imagesCache setName:#"imagesCache"];
[imagesCache setObject:customScreenShot forKey:#"image1"];
the good news is that NSCache accepts any object of type id so you can store images, mp3s, whatever you want.
If you would like to use the phone's file directory (which is more expensive to use than NSCache..) I recommend you use the Library/Caches directory instead of the Documents directory.. so building on iDevzilla's answer:
NSString *documentDirectory =
[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
objectAtIndex:0];
I have been trying to get a double picker to show both text and images. the left picker will have text and the right will have images. When I do text, it works fine but when I try to make images appear I get an error that says
0x6b8b230> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key column1.'
I got this so far but Im stuck and have no idea what to do.
#property (strong, nonatomic) NSArray *textvals;
#property (strong, nonatomic) NSArray *imagevals;
#synthesize textvals;
#synthesize imagevals;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *totalArray = [[NSArray alloc] initWithObjects:#"Text 1",
#"text2", #"text 3", #"text 4", nil];
self.textvals = totaltextArray;
UIImage *image1 = [UIImage imageNamed:#"first.png"];
UIImage *image2 = [UIImage imageNamed:#"second.png"];
UIImage *image3 = [UIImage imageNamed:#"third.png"];
UIImage *image4 = [UIImage imageNamed:#"fourth.png"];
UIImageView *image1View = [[UIImageView alloc] initWithImage:image1];
UIImageView *image2View = [[UIImageView alloc] initWithImage:image2];
UIImageView *image3View = [[UIImageView alloc] initWithImage:image3];
UIImageView *image4View = [[UIImageView alloc] initWithImage:image4];
NSArray *totalimagearray = [[NSArray alloc] initWithObjects:image1View, image2View, image3View, image4View,nil];
//[self setValue:totalimagearray forKey:"1"];
self.imagevals = totalimagearray;
}
yes it is absolutely possible. u need to handle pickerView delegate method
(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
here you simply return custom view(could be anything UIImage UILabel)
and set userInteractionEnable property to no for customize view..
I have the following code:
#interface NeighborProfileViewController : UIViewController {
UIImageView * profPic;
UITextView * text;
UIButton * buzz;
NSString * uid;
NSMutableData *responseData;
NSDictionary * results;
}
#property (nonatomic, retain) IBOutlet UIImageView * profPic;
#property (nonatomic, retain) IBOutlet UITextView * text;
#property (nonatomic, retain) IBOutlet UIButton * buzz;
#property (nonatomic, retain) NSString * uid;
#end
Here's what I have in the viewDidLoad on the .m file
#implementation NeighborProfileViewController
#synthesize uid;
#synthesize profPic;
#synthesize buzz;
#synthesize text;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = //some URL to the picture;
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];
self.profPic = [[UIImageView alloc] initWithImage: img];
}
I think I wired up the UIImageView via IB correctly. I have an outlet from the UIImageView to the profPic in the Files Owner. What am I doing wrong?
If you are setting a default image is that staying visible after you call self.profPic = [UIImageView] or is it being removed from the screen?
I think this problem comes when self.profPic releases the old image view to replace it with the one you've just created. The old UIImageView instance, along with all the properties which you defined in IB, are probably automatically removed from the superview. Which is why you're not seeing the downloaded image.
If you used IB to create the UIImageView then you don't want to create a new ImageView and assign it to profPic (which is already a fully instantiated UIImageView). Try calling [profPic setImage:img]; which will just change the image in the profPic imageview.
#Equinox use
[profPic setImage:img];
instead of
self.profPic = [[UIImageView alloc] initWithImage: img];
although using autorelease is not an issue here.
you can also do something like this
UIImage *img = [[UIImage alloc] initWithData:data] ;
[profPic setImage:img];
[img release];
the problem with
self.profPic = [[UIImageView alloc] initWithImage: img];
is that you are now creating another memory location for profPic and that means profPic will no longer point to the UIImageView in your IB any more