How to set a value for an image - image

I am using a switch statement to fill imageviews with pictures at random.
Now I am trying to give the pictures a value. That way I can use that value to make comparison. Can somebody explain me how to give images a value?
I have used the following code to setup the array for the UIImages, but can't get it completely working: (.m file)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *image1 = [UIImage imageNamed:#"image-1.png"];
UIImage *image2 = [UIImage imageNamed:#"image-2.png"];
UIImage *image3 = [UIImage imageNamed:#"image-3.png"];
UIImage *image4 = [UIImage imageNamed:#"image-4.png"];
UIImage *image5 = [UIImage imageNamed:#"image-5.png"];
_imageArray = #[image1, image2, image3, image4, image5];
}
-(void)displayImageRandomlyOnView {
NSUInteger randomIndex = arc4random_uniform([_imageArray count]);
_imageToDisplay.image = [_imageArray objectAtIndex:randomIndex];
}
Have created also a button in the .h file, to call the view...but can't get the code working for the button calling the view.
If I now try to run the IS it crashes even without the button in it. Can somebody tell me what is going wrong?
The following information is in the .h file: (have left the button out)
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *imageToDisplay;
#property (strong, nonatomic) NSArray *imageArray;
That was really helpful! I have now created a button that will fill in total 10 UiImage views. 5 in the left side of the screen, UiImage view 1-5 and right side I have UiImage view 6-10. I created two separe array's to fill the right and the left side. The only thing I now want to do is to compare the UiImage view 1-5 against 6-10. If a UiImage view is filled with an image it should return value 1 and if not it should have 0. This way I can compare if the right or left side has more images filled or exactly the same. Can you point me in the right direction on how to do this? Thanks!

I would suggest storing the UIImages in an array, sorting them randomly, and then sequentially fill the UIImageViews with the images. This way you don't need to attach a value to the images as you know what position they are in the array from their position amongst your image views.
EDIT
You should replace the line
_imageArray = #[image1, image2, image3, image4, image5];
with
_imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, image4, image5, nil];
What you had would cause an error like this in the Xcode console:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
EDIT 26/11/13
If you have a list of images on the screen and want to check programatically if they are loaded with images, I would suggest using an array of UIImageViews instead of an array of UIImages. Then,
// Initialise the 5 UIImageViews with the UIImages that you already initialised.
UIImageView *image1 = [[UIImageView alloc] initWithImage:image1];
UIImageView *image2 = [[UIImageView alloc] initWithImage:image2];
..
..
UIImageView *image5 = [[UIImageView alloc] initWithImage:image1];
// Put all the UIImageViews in an array.
_imageArray = [[NSArray alloc] initWithObjects:image1, image2, image3, image4, image5, nil];
// Make this method to count the number of images in the given array.
- (int)numberOfImagesIn:(NSArray *)array {
int count = 0;
for (UIImageView *view in array) {
if (view.image) {
count++;
}
}
return count;
}

Related

UIImage Animation Reverting Back To Original Position

Im pretty new to coding but I'm starting to get the hang of the basics.
how to make an image stay in its new position after an animation?
Example:
I'm giving an animating object a random position, however, the animation causes the object not to animate at the random position, but instead animate at the position it was given in the view controller. This also happens when I animate a completly different object.
Code I used:
int Random1x;
int Random1y;
IBOutlet UIButton *Start;
IBOutlet UIImageview *Object2;
-(void)ObjectMoving;
-(void)Object2Animate;
-(IBAction)Start:(id)sender{
[self ObjectMoving];
[self Object2Animate];
}
-(void)Object2Animate {
Object2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed: #"3.png"],
[UIImage imageNamed: #"4.png"],
[UIImage imageNamed: #"1.png"], nil];
Object2.animationDuration = .5
[Object2 setanimationRepeatCount: 0]
[Object2 startAnimating];
}
-(void)ObjectMoving {
Random1y = arc4random() % 466;
Random1y = Random1y + 60;
Random1x = arc4random() % 288;
Object2.center = CGPointMake(Random1x, Random1y);
}
I'd greatly appreciate help, thank you!
If you go to your story board file and click on the View Controller and then file inspector you will see a box for Auto Layout
Post back if that worked.
If you do need to use auto layout then you would have to figure out a different way of moving the image.

Resize UIImageView in TableViewCell

I have UIImageView in my TableViewCell. This Image parse from for example apple JSON. Image show and all work perfectly but i have problem with resize this image in cell. I tried this but nothing. I read many topics and peoples have the same problem. I know how to make size for picture from URL but i have parse image and paste to cell.imageView.
So my code of image from JSON:
[cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:#"artworkUrl60"]] placeholderImage:[UIImage imageNamed:#"noholder.png"]];
And this code i tested but nothing to change in size of picture in cell
cell.imageView.frame=CGRectMake(50,50,300,300);
How to change the size of image in cell but without URL link. I must change size picture from cell.Thanks.
You'll need to create a custom cell (at least for iOS 7).
Here's a sample:
.h
#interface UITableViewImageCell : UITableViewCell {
CGRect _imageViewFrame;
}
/**
* To use default, set width and height to 0
*/
#property(nonatomic, assign) CGRect imageViewFrame;
#end
.m
#implementation UITableViewImageCell
#synthesize imageViewFrame=_imageViewFrame;
-(void)setImageViewFrame:(CGRect)imageViewFrame{
_imageViewFrame = imageViewFrame;
[self setNeedsLayout];
}
-(void)layoutSubviews{
[super layoutSubviews];
if (_imageViewFrame.size.width>0 && _imageViewFrame.size.height>0) {
self.imageView.frame = _imageViewFrame;
}
}
#end
Using this class, you'd have to call:
cell.imageViewFrame=CGRectMake(50,50,300,300);

Retaining array in subclass

Updated:
I have subclassed UIImageView to make some images movable through touch gestures. In the view controller I have an array holding the initial position of each imageview. My problem is that this array returns null whenever it is called on from the subclass. The idea is to check if the image is in its original position or if it has already been moved before, I have stripped the code to just NSLog what's going on but the problem remains and is driving me nuts.
ViewController.h
NSMutableArray *originalPositions;
#property (nonatomic, retain) NSMutableArray *originalPositions;
-(void)testLogging;
ViewController.m
#synthesize originalPositions;
- (void)viewDidLoad {
originalPositions = [[NSMutableArray alloc]init];
}
-(void)drawImages {
for (int i = 0; i < imagesArray.count; i++) {
CGRect frame = CGRectMake(65 * i, 10, 60, 60);
draggableView *dragImage = [[draggableView alloc] initWithFrame:frame];
NSString* imgName = [imagesArray objectAtIndex:i];
[dragImage setImage:[UIImage imageNamed:imgName]];
[dragImage setUserInteractionEnabled:YES];
[self.view addSubview:dragImage];
NSString *originalPositionAsString = NSStringFromCGPoint(dragImage.center);
[originalPositions addObject:originalPositionAsString];
}
}
-(void)testLogging {
NSLog(#"Logging array: %#", originalPositions);
}
-(IBAction)btnClicked {
[self testLogging];
}
When called from the IBAction (or any other way) in the class, the 'testLogging'-method NSLogs the array correctly but if I call the same method from the subclass, it NSLogs null:
Subclass.m
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
ViewController *viewController = [[ViewController alloc] init];
[viewController testLogging];
}
self.originalPositions = [[NSMutableArray alloc]init];
This already is a memory leak, when you call self.originalPositions = bla then 'bla' will be retained. Thus meaning that the retain count is upped. This means that your mutable array will have a retain count of 2 and that it will leak once your UIImageView is gone.
As for the rest, I wouldn't know what is wrong with your code. My first guess would be that you didn't call populatePositionsArray yet, you should call this whenever your view is created / shown so you are sure the array is populated when touchesBegan is called.
Alternatively you could include an 'if' statement in the touchesBegan that checks whether the array exists and otherwise call the populatePositionsArray to populate it before continueing.
I solved it by writing the array to a plist file after populating it. Not very elegant but working.
Thanks for the help though guys.

Random facebook profile id generator

Right I have a programme that displays a profile facebook profile picture when a button is touched accessed by using the graph api as shown. in the url:
http://graph.facebook.com/517418990/picture?type=large
The number can be changed to show a different profile picture (some numbers dont work but that is a different question. How in xcode can i firstly generate a random number (i assume it has to have some kind of structure not just a random 9 digit number) and secondly how can i load a new random number with each push of the button. E.g. push button = display profile of random number, push 2 = display profile of new random number etc etc. Some of the code below is not completely relevant because i also have an array with some images in it that to go through. Thanks in advance!
#interface FacebookPicturesViewController : UIViewController {
IBOutlet UIImageView *imageView;
NSInteger imageCount;
NSArray *imageArray;
NSString *profileLink;
}
- (IBAction) nextImagePush;
#end
#implementation FacebookPicturesViewController
(IBAction) nextImagePush {
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://graph.facebook.com/517418990/picture?type=large"]]];
[imageView setImage:img];
if (imageCount >= [imageArray count]){
imageCount = 0;
}
}
(void) viewDidLoad {
imageArray = [[NSArray alloc] initWithObjects:#"image1", #"image2", nil];
imageCount = 0;
}

Xcode : Wallpapers with UIButton

I want to make an wallpaper iphone app.
Now if i have for example 100 wallpapers , do I have to create 100 nib files and 100 .m and .h files and 100 UIButtons for to save the wallpaper and 100 UIButton for to go to the previous or next wallpaper ?
Is there any other way to do this ?
Thanks !
yes sure.
save your hundred wallpapers in the resources folder. The are named like this "wp_1.png", "wp_2.png" etc.
Then you create a nib with the buttons (previous, next and save)
In the uiviewcontroller you'll have an int property. For instance you call it n or something like that. I would suggest adding another property for your wallpaper (UIImageView) Then you implement the methods in the view controller.
-(void)awakeFromNib {
self.n = 1;
UIImage* wallpaper = [UIImage imageNamed:#"wp_1.png"];
UIImageView* view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.wallpaper = view;
view.image=wallpaper;
[self.view addSubview:view];
}
-(IBAction)next:(id)sender {
self.n++; // increases n by 1;
[self.wallpaper removeFromSuperview];
NSString* name = [NSString stringWithFormat:#"wp_%i.png", self.n];
UIImage* wallpaper = [UIImage imageNamed:name];
UIImageView* view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.wallpaper = view;
view.image=wallpaper;
[self.view addSubview:view];
}
something like that. hope that helps
Of course not. I would use a UITableView for that. Or even better a custom view that looks similar to the Photos app on the iphone.
You don't want to use a UIButton to go to the previous or next photo. You want to use a UIScrollView in paging mode.
But you don't want the user to browse through your images in that way in the first place.
Create a index screen that has a UIScrollView (non-paging this time) with thumbnails of all your images. And when one is tapped it zooms in with a nice animation and you can flick to the next or previous image with the paging UIScrollView I mentioned earlier.
You know, like the Photos app.
in the header (.h file) add something like that:
int n;
UIImageView* wallpaper;
}
#property (readwrite) int n;
#property (readwrite, retain) UIImageView* wallpaper;
in the implemention fil (.m file) :
#synthesize n, wallpaper;

Resources