i have two ViewControllers, when i tap a cell i need to open other viewController and see image (image with names 1, 2, 3,...._full.jpg), so i write:
DetailViewController *dvc = [[DetailViewController alloc] init];
[dvc updateImage:[NSString stringWithFormat:#"%d", indexPath.row]];
[self.navigationController pushViewController:dvc animated:YES];
in my dvc
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
[self.image setTag:1];
[self.view addSubview:self.image];
}
method what i called
-(void)updateImage:(NSString*)imageName
{
[(UIImageView*)[self.view viewWithTag:1] setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#_full.jpg", imageName]]];
}
smth like self.image.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#_full.jpg", imageName]]; didn't work for me.
so, these code works fine in my IOS 5,6,7 emulators, but when i compile it on my IPad 2, IOS 5.1 my image is not updating, all i see is gray background.
What am i doing wrong?
You run
[dvc updateImage:[NSString stringWithFormat:#"%d", indexPath.row]];
After that viewDidLoad is called and your image is reseted.
Instead that pass image name as a string
[dvc setImageName:[NSString stringWithFormat:#"%d", indexPath.row]];
and in DetailViewController.m in viewDidLoad call
[self updateImage:[NSString stringWithFormat:#"%#_full.jpg", self.imageName]];
Remember to add #property imageName to your DetailViewController.h file.
omg, XCode is joking, it's all because of my images where not ".jpg" they where ".JPG"!. I'm angry...
Related
- (void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:#"11.jpg",#"22.jpg",#"33.jpg", nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
}
When I put the NSMutableArray in -(void)viewDidLoad, UIImageView displays nothing and NSLog shows NULL. Why is that?
ps. NSMutableArray worked perfectly in -(void)loadView. I've declared NSMutableArray *arrayOfImage in #interface .h file
The only way that the given could would output "NULL" in this situation is that arrayOfImages is NULL by itself.
This is only possible if arrayOfImages is declared as a weak variable.
But as #maddy pointed out, your code is all wrong: Don't call super, but assign self.view. Or use viedDidLoad instead (probably what you want here).
-(void)loadView
{
[super loadView];
arrayOfImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"11.jpg"],[UIImage imageNamed:#"22.jpg"],[UIImage imageNamed:#"33.jpg"], nil];
UIImageView *awesomeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[awesomeView setImage :[UIImage imageNamed:[arrayOfImages objectAtIndex:0]]];
awesomeView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:awesomeView];
NSLog(#"%#",[arrayOfImages objectAtIndex:0]);
This will work but you will only get to see 1 picture. ie the picture in the 0 index of the array.
If you want to show all then you got to apply a loop.
I have a segmented controll with two cells defined programmatically. When I go into my app both cells perform the same action. The first should open a webpage in Safari, the second opens an image and covers the current view for 5 seconds. Any pointers?
In the .m file
#property UISegmentedControl *segment;
- (void)viewDidLoad
{
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Publication", #"About", nil]];
self.tableView.tableHeaderView = segment;
[segment addTarget:self action:#selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:#"UITableViewCell"];
}
- (void)segmentPressed:(id)sender {
if (_segment.selectedSegmentIndex ==0) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"******"]];
}else if(_segment.selectedSegmentIndex ==1){
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage: [UIImage imageNamed:#"MACSLoad#2x.png"]];
[self.view addSubview: imageView];
sleep(5);
imageView.hidden = YES;
}
}
You get that result because _segment is nil. You never assigned the segmented control you created to your property -- you assigned it to a local variable. So change this line,
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Publication", #"About", nil]];
to,
self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Publication", #"About", nil]];
Another way to do it, would be to get rid of the property all together, leave the code in viewDidLoad as it is, and change this,
- (void)segmentPressed:(id)sender {
if (_segment.selectedSegmentIndex ==0) {
to this,
- (void)segmentPressed:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex ==0) {
Unless you need to access the segmented control outside of its action method, there's no reason to create the property. It's better in any case to use the sender argument rather than a property (even if you have one) inside the action method.
I am using iCarousel custom control to show image from web that consumed with JSON data.
Here is my codes to show image in iCarousel
to Load JSON Data in ViewDidLoad
JSONLoader *jsonLoader = [[JSONLoader alloc]init];
self.items = [[NSMutableArray alloc]init];
[self.items removeAllObjects];
self.items = (NSMutableArray *) [jsonLoader loadJSONDataFromURL:[NSURL URLWithString:#"https://public-api.wordpress.com/rest/v1/sites/www.myWebsite.com/posts?category=blog&page=1"]];
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(AsyncImageView *)view
{
MMSPLoader *mmObject = [self.items objectAtIndex:index];
view = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, 250.0f, 250.0f)];
view.layer.borderColor = [UIColor whiteColor].CGColor;
view.layer.borderWidth = 0.3f;
view.image=[UIImage imageNamed:#"page.png"];
view.imageURL = [NSURL URLWithString:[mmObject featureImageUrl]];
return view;
}
That can show image correctly. My case is when i tap on that image , i want to show that image in FULL SCREEN. So i used GGFullScreenImageViewController.
However when i tap on that Image to show FULL SCREEN , i retrieved Image URL and show in GGFullScreenImageViewController. It's fine but , i don't want to retrieve from that URL because it downloading image from web again and slowing to show.
In my idea , i saved that image when tap on image in iCarousel and show it in GGFullScreenImageViewController.
So i don't need to download image again.
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
dispatch_queue_t myqueue = dispatch_queue_create("com.i.longrunningfunctionMain", NULL);
dispatch_async(myqueue, ^{
UIApplication *apps = [UIApplication sharedApplication];
apps.networkActivityIndicatorVisible = YES;
MMLoader *mmObject = [self.items objectAtIndex:index];
NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mmObject.featureImageUrl]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
dispatch_async(dispatch_get_main_queue(), ^{
apps.networkActivityIndicatorVisible = NO;
[self presentViewController:vc animated:YES completion:nil];
});
});
NSLog(#"%i",index);
}
So should i save to local file or is there any others nice idea?
Really you should use a library to save the image when you initially download it. AsyncImageView isn't necessarily the best choice as it just caches in memory.
That said, at the moment you can just get the image from the view. This isn't ideal, and you should save it to disk - just sooner rather than later. Look at, perhaps, SDWebImage for that.
To get the image from the view (typed in browser so verify syntax and API usage...):
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
AsyncImageView *view = (AsyncImageView *)[carousel itemViewAtIndex:index];
UIImageView *imageView = [[UIImageView alloc] initWithImage:view.image];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
[self presentViewController:vc animated:YES completion:nil];
}
I am trying to move a UIImage, first button press creates the image and second press moves it.
The image only needs to exist upon pressing the button.
In the simulator it creates the button and places it, the second time it click just doesn't do anything.
This is my Code
- (IBAction) btn:(id)sender {
UIImageView *myImage = [[UIImageView alloc] init];
myImage.image = [UIImage imageNamed:#"keyframe"];
if (startUp == 1){
//Create Image and add to view
myImage.frame = CGRectMake(200, 300, 10, 10);
myImage.image = [UIImage imageNamed:#"keyframe"];
[self.view addSubview:myImage];
//Set startUp to 0 and output rect value
startUp = 0;
NSLog(#"currentFrame %#", NSStringFromCGRect(myImage.frame));
}else if (startUp == 0){
//Change position, size and log to debug
myImage.frame = CGRectMake(500,100 ,20, 20);
NSLog(#"newFrame %#", NSStringFromCGRect(myImage.frame));
}
}
How do you programmatically move a programmatically added UIimage?
I tried changing the center value but that doesn't work either.
Try something like this – tested and working sample:
#import "ViewController.h"
#interface ViewController () {
BOOL startUp;
UIImageView *myImage;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
startUp = YES;
}
- (IBAction)doWork:(id)sender {
if (startUp) {
UIImage *img = [UIImage imageNamed: #"keyframe"];
myImage = [[UIImageView alloc] initWithImage: img];
[myImage sizeToFit];
[myImage setCenter: CGPointMake(200, 300)];
[self.view addSubview: myImage];
startUp = NO;
} else {
[myImage setCenter: CGPointMake(400, 500)];
}
}
#end
I have many view controller when i click on tableView cell it move to new view controller problem is that it takes alot of time to move to the next view may be due to view which is to load fetches data from server here is my code for the view which loads
- (void)viewDidLoad {
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
UIImageView *bottomImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Nav.png"]];
[bottomImageView setFrame:CGRectMake(0,690,1024,34)];
[self.view addSubview:bottomImageView];
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(10.0, 2.0, 88, 40.0)];
[button1 addTarget:self action:#selector(loginPressed) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"logoutN.png"] forState:UIControlStateNormal];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.navigationItem.rightBarButtonItem = button;
self.title=#"Catalog";
popImageView.hidden=YES;
passwordLabel.hidden=YES;
userLabel.hidden=YES;
userNameTextField.hidden=YES;
userPasswordTextField.hidden=YES;
signInButton.hidden=YES;
tableView.hidden=NO;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
if(!categoryArray){
categoryArray =[[NSMutableArray alloc] init];
}
if(!userArray){
userArray =[[NSMutableArray alloc] init];
}
if(!subCategoryArray){
subCategoryArray =[[NSMutableArray alloc] init];
}
if(!subCategoryArrayOne){
subCategoryArrayOne =[[NSMutableArray alloc] init];
}
if(!subCategoryArrayTwo){
subCategoryArrayTwo =[[NSMutableArray alloc] init];
}
[self setUpData];
[self setUpDataSub];
[self setUpDataSubOne];
[self setUpDataSubTwo];
int count=[appDelegate.coffeeArray count];
NSLog(#"Arrays Content Are %d",count);
tableView.backgroundView = nil;
[super viewDidLoad];
}
is there any way so that view loads fast
Your view is not loading fast because of I guess those data set operation in viewDidLoad method . I think those are :
[self setUpData];
[self setUpDataSub];
[self setUpDataSubOne];
[self setUpDataSubTwo];
The one thing you could do is to move this operations to perform on the background thread . For that move this operations to the separate function and call that to perform on background from the view did load method :
-(void)dataOperations
{
[self setUpData];
[self setUpDataSub];
[self setUpDataSubOne];
[self setUpDataSubTwo];
}
and in viewDidLoad call this function in background:
[self performSelectorInBackground:#selector(dataOperations) withObject:nil];
Or you can directly call those method from viewDidLoad like :
[self performSelectorInBackground:#selector(setUpData) withObject:nil];