I want to put defaults images if i don't have images from url feed.
if(![[stories objectAtIndex:storyIndex] valueForKey:#"url"]==nil)
{ NSNumber *numberOfKeyInstances = [stories valueForKey:#"key"];
imageArray=[NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"],nil];
int randomNumber_ = rand ()%19;
UIImage *image = [imageArray objectAtIndex:indexPath.row];
cell.imag=image;
}
else{
if (![[[stories objectAtIndex:storyIndex] valueForKey:#"url"] isEqualToString:#""]) {
[cell.imag setImageWithURL:[NSURL URLWithString:[[stories objectAtIndex:storyIndex] objectForKey:#"url"]] placeholderImage:[UIImage imageNamed:#"Alb.png"]];
}
}
This is my code, but i get [UIImage setContentMode:]: unrecognized selector sent to instance 0x7f908c1dcce0'
How can i solved?
Firstly, imageArray is initialised with only one image. So calling [imageArray objectAtIndex:indexPath.row] is a problem. You dont need an image array for that. Secondly, the argument inside the first if statement doesn't look right.
Also, as #midhun-mp Pointed out, u should be probably using cell.imag.image.
Try this :
if([[stories objectAtIndex:storyIndex] valueForKey:#"url"] == nil)
{
cell.imag.image = [UIImage imageNamed:#"1.png"];
}
else if ([[[stories objectAtIndex:storyIndex] valueForKey:#"url"] isEqualToString:#""])
{
cell.imag.image = [UIImage imageNamed:#"1.png"];
}
else
{
//SET YOUR IMAGE HERE
}
Try to change
UIImage *image = [imageArray objectAtIndex:indexPath.row];
to
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:[imageArray objectAtIndex:indexPath.row]]];
I suspect that cell.imag is a UIImageView not UIImage property.
So you need to change:
cell.imag=image;
to
cell.imag.image = image;
Related
I am very new to xcode and I am playing around with image arrays and NSMutableArray. When I run the code below it just print out the names of the image for example "image1.png". Any tips to fix this problem would be appreciated. Thanks.
- (NSMutableArray*)restaurantArray;
{
if (_restaurantArray == nil) {
_restaurantArray = [[NSMutableArray alloc] initWithObjects:
#"image1.png",
#"image2.png",
#"image3.png",
nil];
}
return _restaurantArray;
}
-(NSString*) randomRestaurant
{
int random = arc4random_uniform(self.restaurantArray.count);
return [self.restaurantArray objectAtIndex:random];
}
You could do it in this way also.
- (NSMutableArray*)restaurantArray;
{
if (_restaurantArray == nil) {
_restaurantArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
nil];
}
return _restaurantArray;
}
Do you mean this?:
NSString *imageName = [self randomRestaurant];
UIImage *image = [UIImage imageName:imageName];
If you want to display the image then do this:
Suppose you have an imageview name imgView,
imgView.image = [UIImage imageNamed:[self randomRestaurant]];
Or, if you just want the image, then call this:
UIImage *img = [UIImage imageNamed:[self randomRestaurant]];
In you given code, you are just returning the name of the image, not the actual image. If you want an array of image then you can do it by this way:
-(NSMutableArray*) restaurantArray;
{
if (_restaurantArray == nil) {
UIImage *img = [UIImage imageNamed:#"image1.png"];
UIImage *img1 = [UIImage imageNamed:#"image2.png"];
UIImage *img2 = [UIImage imageNamed:#"image3.png"];
_restaurantArray = [[NSMutableArray alloc] initWithObjects:
img,
img1,
img2,
nil];
}
return _restaurantArray;
}
-(UIImage*) randomRestaurant
{
int random = arc4random_uniform(self.restaurantArray.count);
return [self.restaurantArray objectAtIndex:random];
}
Hope this helps.. :)
I am trying to create an app like this:
(source: topmobiletrends.com)
where each of the views show an image for every in a photo in a JSON array.
I have successfully set and downloaded the array of images and set them in a UIImageView with with NSURLRequest and SDWebImage.
I created a UIView with an image view inside to test the code in my ViewController(Storyboard), everything works fine. However, when I try to create a UIView to loop programmatically, it shows up blank, with no ImageView. I'm not sure what I'm doing wrong.
Here is my code for my ViewController.m
for (NSObject *photo in photos) {
UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
dView.backgroundColor = [UIColor whiteColor];
dispatch_async(dispatch_get_main_queue(),^{
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
myImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//Get image from url
[self.imageView setImageWithURL:imageURL];
[self.myImage addSubview:imageView];
priceLabel.text = [[[jsonDictionary objectForKey:#"site"] objectAtIndex:index] objectForKey:#"price"];
[imageView addSubview:dView];
});
}
}];
[task resume];
}
you are adding dView as subview for your image view may be it is [dView addSubview:imageView];
try this. the main issue is you are not setting frame for image view
for (NSObject *photo in photos) {
//UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
//dView.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(160,250,258,229)];
[self.myImage addSubview:imageView];
priceLabel.text = [[[jsonDictionary objectForKey:#"site"] objectAtIndex:index] objectForKey:#"price"];
//[imageView addSubview:dView];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"URL"]];
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData) {
[imageView setImage:[UIImage imageWithData:imgData]];
}
});
});
}
is it possible to manage multiple NSArrays on a different View Controller?
Here's my example:
I am currently working with two View Controllers:
TableViewController serves as a main menu. I've set up multiple UIButtons, each has a tag and when the user presses a button, it'll take them to the TableOneViewController.
In TableOneViewController, so far I've set up an UIImageView and a UIButton, every time the user presses the button, the UIImageView changes the image through an NSArray.
I want to use the tags on the TableViewController to manage the NSArrays of the TableOneViewController. Is this possible? So far, my code is not working.
Here's the code. Thanks!
TableViewController.m
- (IBAction)tableSelection:(id)sender
{
TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TableOneViewController"];
if ([sender tag] == 1) {
[[NSUserDefaults standardUserDefaults] setObject:TOVC.numOne forKey:#"num"];
[[NSUserDefaults standardUserDefaults] setObject:#"1.png" forKey:#"num2"];
}
if ([sender tag] == 2) {
[[NSUserDefaults standardUserDefaults] setObject:TOVC.numTwo forKey:#"num"];
[[NSUserDefaults standardUserDefaults] setObject:#"2.png" forKey:#"num2"];
}
if ([sender tag] == 3) {
[[NSUserDefaults standardUserDefaults] setObject:TOVC.numThree forKey:#"num"];
[[NSUserDefaults standardUserDefaults] setObject:#"3.png" forKey:#"num2"];
}
[self presentViewController:TOVC animated:YES completion:nil];
}
TableOneViewController.m
- (IBAction)nextButton:(id)sender
{
int index = tableCounter++ % [self.numOne count];
self.num.image = self.numOne[index];
}
- (void)viewDidLoad
{
self.num.image = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] objectForKey:#"num"]];
self.num2.image = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] objectForKey:#"num2"]];
[super viewDidLoad];
tableCounter = 10;
self.numOne = [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"1.png"],
nil];
self.numTwo = [NSArray arrayWithObjects:
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"40.png"],
[UIImage imageNamed:#"50.png"],
[UIImage imageNamed:#"60.png"],
[UIImage imageNamed:#"70.png"],
[UIImage imageNamed:#"80.png"],
[UIImage imageNamed:#"90.png"],
[UIImage imageNamed:#"100.png"],
[UIImage imageNamed:#"10.png"],
nil];
self.numThree = [NSArray arrayWithObjects:
[UIImage imageNamed:#"200.png"],
[UIImage imageNamed:#"300.png"],
[UIImage imageNamed:#"400.png"],
[UIImage imageNamed:#"500.png"],
[UIImage imageNamed:#"600.png"],
[UIImage imageNamed:#"700.png"],
[UIImage imageNamed:#"800.png"],
[UIImage imageNamed:#"900.png"],
[UIImage imageNamed:#"1000.png"],
[UIImage imageNamed:#"100.png"],
nil];
}
The problem is that you pass the array before presenting the view controller (viewDidLoad hasn't be called yet), then you present it, but you initialize numOne (and the other arrays) in viewDidLoad. I suggest to use lazy initialization, here's the example for numOne, write the same for the other two arrays:
- (NSArray*) numOne
{
if(!_numOne)
{
_numOne= [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"1.png"],
nil];
}
return _numOne;
}
However this is not the purpose of NSUserDefaults, I consider this approach bad design. It's better if you keep an ivar in TableOneViewController that points to one of the three arrays (same for the image name).
Edit
This is how I would implement the mechanism: an additional property that points to one of the three arrays in TableOneViewController:
#property (nonatomic, weak) NSArray* currentArray;
This pointer should point to the correct array, according to the tag of the sender that triggered tableSelection: :
// TableViewController
- (IBAction)tableSelection:(id)sender
{
TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TableOneViewController"];
switch([sender tag]) {
case 1:
TOVC.currentArray= TOVC.numOne;
break;
case 2:
TOVC.currentArray= TOVC.numTwo;
break;
case 3:
TOVC.currentArray= TOVC.numThree;
break;
default:
return;
}
[self presentViewController:TOVC animated:YES completion:nil];
}
// TableOneViewController
- (IBAction)nextButton:(id)sender
{
int index = tableCounter++ % [self.currentArray count];
self.num.image = self.currentArray[index];
}
It doesn't work because
TableOneViewController *TOVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TableOneViewController"];
creates a completely new instance of your TableOneViewController. I would use a singleton retaining the arrays and any other info I need as a single access point during the execution of my program.
I have made an app, but on ipad 1 it crashes in the end part. On ipad-higher it remains working, but still I think I am doing something wrong, I really need to make something working more economically. But what is it?
In the end part it all happens. Before that, several screens are put onto each other and in the simulator when you go back they all are dismissed. But on the ipad 1 you will not even reach the end of the end part. I will try to explain what happens and several idea's about what is happening I have.
You can swipe on the screen and retina images (big images) are exchanged with the four corners that can get selected, each with a different image-background. There are six different scenes, so there are 6x4 + 6x1 for-normal-0-situation, 25 big retina images. There are only 5 frames working on top of each other (4 corners and 1 background), but I set inactive stuff to nil and load active images with imageNamed. It goes well on the simulator, but it crashes on the ipad 1, with no low memory warning or what so ever.
I've studied improvements: a) autorelease pool b) imageNamed replaced by imageContentsOfFile.
The autorelease pools don't have any effect. imageNamed, I couldn't get imageContentsOfFile working, so I skipped the operation.
Any ideas of what is so excessively wrong and what kind of thoughts can be applied to have it work more economically, for it somewhere is demanding way too much in the end.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// Get the subviews of the view
CGPoint tappedPt = [[touches anyObject] locationInView:self.view];
NSArray *subviews = [[NSArray alloc]init];
subviews = [self.view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
if ([subview pointInside:[self.view convertPoint:tappedPt toView:subview] withEvent:event]){
[self checkAndChange:subview];
[self modifyChoice:subview];
}
}
}
-(void) modifyChoice:(UIView *)subview
{
if(subview==area1LinksBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:1]];}
if(subview==area2RechtsBoven){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:2]];}
if(subview==area3RechtsOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:3]];}
if(subview==area4LinksOnder){[gekozenExpressieBijDimensie replaceObjectAtIndex:dimensie-1 withObject:[NSNumber numberWithInteger:4]];}
if(maxChoice<dimensie){maxChoice=dimensie;
if(maxChoice==1){headerGevuld.image = [UIImage imageNamed:#"TOP1.png"];}
if(maxChoice==2){headerGevuld.image = [UIImage imageNamed:#"TOP2.png"];}
if(maxChoice==3){headerGevuld.image = [UIImage imageNamed:#"TOP3.png"];}
if(maxChoice==4){headerGevuld.image = [UIImage imageNamed:#"TOP4.png"];}
if(maxChoice==5){headerGevuld.image = [UIImage imageNamed:#"TOP5.png"];}
if(maxChoice==6){headerGevuld.image = [UIImage imageNamed:#"TOP6.png"];}
}
}
-(void) checkAndChange:(UIView *)subview{
if(subview==area1LinksBoven){
area1LinksBoven.image = [UIImage imageNamed:#"SELECTION1.png"];
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT1.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"1PassiefActiefOntspanning.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"1MakkelijkMoeilijkMaster.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"1VeiligGevaarlijkGeborgenheid.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"1NormaalBijzonderGeaard.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"1AlleenSamenUniek.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"1OrdeChaosDuidelijk.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area2RechtsBoven){
area1LinksBoven.image = nil;
area2RechtsBoven.image = [UIImage imageNamed:#"SELECTION2.png"];
area3RechtsOnder.image = nil;
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT2.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"2PassiefActiefEnergiek.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"2MakkelijkMoeilijkUitdagend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"2VeiligGevaarlijkSpannend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"2NormaalBijzonderWow.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"2AlleenSamenGezellig.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"2OrdeChaosImproviseren.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area3RechtsOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = [UIImage imageNamed:#"SELECTION3.png"];
area4LinksOnder.image = nil;
footer.image = [UIImage imageNamed:#"FOOT3.png"];
#autoreleasepool {
if(dimensie==1){
filmAchtergrond0.image = [UIImage imageNamed:#"3PassiefActiefUitputting.jpg"];}
if(dimensie==2){
filmAchtergrond0.image = [UIImage imageNamed:#"3MakkelijkMoeilijkFrustrerend.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"3VeiligGevaarlijkGevaarlijk.jpg"];}
if(dimensie==4){
filmAchtergrond0.image = [UIImage imageNamed:#"3NormaalBijzonderOnbegrijjpelijk.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"3AlleenSamenGroepsdruk.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"3OrdeChaosRommeltje.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(subview==area4LinksOnder){
area1LinksBoven.image = nil;
area2RechtsBoven.image = nil;
area3RechtsOnder.image = nil;
area4LinksOnder.image = [UIImage imageNamed:#"SELECTION4.png"];
footer.image = [UIImage imageNamed:#"FOOT4.png"];
#autoreleasepool {
if(dimensie==1){filmAchtergrond0.image = [UIImage imageNamed:#"4PassiefActiefApathie.jpg"];}
if(dimensie==2){filmAchtergrond0.image = [UIImage imageNamed:#"4MakkelijkMoeilijkSaai.jpg"];}
if(dimensie==3){
filmAchtergrond0.image = [UIImage imageNamed:#"4VeiligGevaarlijkBeklemmend.jpg"];}
if(dimensie==4){filmAchtergrond0.image = [UIImage imageNamed:#"4NormaalBijzonderSleur.jpg"];}
if(dimensie==5){filmAchtergrond0.image = [UIImage imageNamed:#"4AlleenSamenEenzaam.jpg"];}
if(dimensie==6){filmAchtergrond0.image = [UIImage imageNamed:#"4OrdeChaosRigide.jpg"];}
}
[arrowRight setImage:[UIImage imageNamed:#"ARROW_RIGHT_ACTIVE.png"] forState:UIControlStateNormal];
}
if(dimensie==1){headerPositie.image = [UIImage imageNamed:#"OUT1.png"];}
if(dimensie==2){headerPositie.image = [UIImage imageNamed:#"OUT2.png"];}
if(dimensie==3){headerPositie.image = [UIImage imageNamed:#"OUT3.png"];}
if(dimensie==4){headerPositie.image = [UIImage imageNamed:#"OUT4.png"];}
if(dimensie==5){headerPositie.image = [UIImage imageNamed:#"OUT5.png"];}
if(dimensie==6){headerPositie.image = [UIImage imageNamed:#"OUT6.png"];}
}
Load the images using UIImage imageWithContentsOfFile:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"fileName" ofType:#"fileExtension"];
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
See here the difference between imageNamed and imageWithContentsOfFile:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html
What syntax would make this work?
NSString *curstring = [cur description];
UIImage *weatherimage = [UIImage imageNamed: #"#%.png", curstring];
[weather setImage:weatherimage];
Thanks
You're looking for NSString's stringWithFormat:
UIImage *weatherimage = [UIImage imageNamed:[NSString stringWithFormat:#"#%.png", curstring]];