Add multiple image in same UIImageView and change them at each tap - image

I'm trying to implement a UIImageView that shows different .png (organized in a NSArray) at each tap. When the tap reaches the last .png, the next tap will show the first .png and so on.
I found an old answer here:
Add 4 image in imageview and change the picture with tap or swipe gesture
that seems to go in the right direction. However, it doesn't seem to be applicable in the new XCode and there is no more possibility to ask clarifications or updates since the topic has been closed.
Does anybody have an idea on how to subclass then entire ImageView to get the expected result?

The solution was much easier than I thought.
As a custom value to the selector cannot be passed in the way I asked, I created an helper property that holds the index of the currently displayed image, increment that on every tap and set the image accordingly.
#interface YourViewController ()
#property (nonatomic, strong) NSArray *images;
#property (nonatomic, assign) NSInteger currentImageIndex;
#end
#implementation YourViewController
- (void)viewDidLoad {
// Here you fill your images array
self.images = ...
// Set currentImageIndex to 0 and display first image
self.currentImageIndex = 0;
[self displayImageWithIndex:self.currentImageIndex];
// Additional code, like your button target/action
}
- (void)myButtonWasTapped:(id)sender
{
if(self.currentImageIndex + 1 < [self.images count]) {
self.currentImageIndex++;
} else {
self.currentImageIndex = 0;
}
[self displayImageWithIndex: self.currentImageIndex];
}
- (void)displayImageWithIndex:(NSInteger)index {
self.imageView.image = [self.images objectAtIndex:index];
}
#end
Please note: there might be a better approach to this, but this is what it worked in my case.
Hope this can be helpful.

Related

How on OS X to programmatically force initial adjustment the size of image in an ImageWell, as ImageWell changes size due to LayoutConstraints

NSWindow
NSImageWell - with constraint to all four side of the Window's containerView
NSImage - is put in Well via NSPageControl
Using the mouse, I resize the Window and the ImageWell expands or contracts with the Window as desire.
The image in the Well remains the same size regardless of Window size.
The image does resize correctly, based on the size change of Window, if I drag the image a bit with the mouse.
How and where do I send a message to get this size to change "automatically" ?
Do I need to monitor size changes in the Window using Notifications?
Sorry, I was not clear. I want the image in the image well to maintain its
aspect ratio and to resize itself each time the Window is resized. Currently, when the window is resized, the user sees no change in the size of the image. He can however, cause the image to resize correctly by click dragging on the image or Window content view. I want the user to see this
resizing take place immediately, without needing to use the mouse. For example of an image that has an aspect ratio of 4/3. if the window is 4" x 4" , then when the image is added, it appears as 4" by 3". If the window goes to 8" x 8", then the image should automatically go to 8" x 6".
Guilherme was good enough to send me his demo NSView app. If, like in his app, I set an image in IB, my app also successfully resizes not only the ImageView but also the Image itself.
The problem appears to be because I put the images into the ImageView via an NSPageController. The only way to get the image to resize is to do a two finger drag inside the window.contentView
After Resizing window:
New Image Appears
Then following a two-finger drag on image :
<https://www.dropbox.com/s/d2he6bdzxbeefok/Screen%20Shot%202015-09-12%20at%2013.26.50.png?dl=0>
Below is relevant code:
AppDelegate follows:
#import "AppDelegate.h"
#import "MyImageView.h"
#interface AppDelegate ()
#property (strong, nonatomic) IBOutlet NSWindow *window;
#property (strong, nonatomic) IBOutlet NSPageController *pageController;
#property (strong, nonatomic) IBOutlet MyImageView *imageView;
#property (weak) IBOutlet NSTextField *infoLabel;
#property NSArray *imageArray;
#end
#implementation AppDelegate
#pragma mark Window delegate
- (void)windowDidResize:(NSNotification *)notification{
//[_window layoutIfNeeded];
//[_imageView doSelfLayout];
}
- (void)awakeFromNib {
[_window setDelegate:self];
[_imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
_imageArray = #[ [NSImage imageNamed:#"eggplant.png"],
[NSImage imageNamed:#"sandwich.png"],
[NSImage imageNamed:#"yoghurt.png"]];
[_pageController setDelegate:self];
[_pageController setArrangedObjects:_imageArray];
[_pageController setTransitionStyle:NSPageControllerTransitionStyleStackBook];
// Set info label's text
NSString *info = [NSString stringWithFormat:#"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
[_infoLabel setStringValue:info];
}
#pragma mark Page Controller delegate methods:
- (void)pageController:(NSPageController *)pageController didTransitionToObject:(id)object {
/* When image is changed, update info label's text */
NSString *info = [NSString stringWithFormat:#"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
[_infoLabel setStringValue:info];
//[_window layoutIfNeeded];
}
- (NSString *)pageController:(NSPageController *)pageController identifierForObject:(id)object {
NSString *identifier = [[NSNumber numberWithInteger:[_imageArray indexOfObject:object]] stringValue];
return identifier;
}
- (NSViewController *)pageController:(NSPageController *)pageController viewControllerForIdentifier:(NSString *)identifier {
/* Create new view controller and image view */
NSViewController *vController = [NSViewController new];
NSImageView *iView = [[NSImageView alloc] initWithFrame:[_imageView frame]];
[iView setImage:(NSImage *)[_imageArray objectAtIndex:[identifier integerValue]]];
[iView setImageFrameStyle:NSImageFrameNone];
/* Add image view to view controller and return view controller */
[vController setView:iView];
return vController;
}
#end
What option have you selected for the scaling property in IB?
Here's a sample project, the image resizes automatically with Proportionally Up or Down selected.
I have made a demo Cocoa project zipfile below that has what I was looking for, namely an OS X app that :
- uses NSPageController in History mode to display an array of images (Portrait and Landscape) in a resizeable window
- imageView maintains imagefile aspect ratio as it automatically resizes
- images list can be traversed using a PreviousButton and a NextButton, or using a touch pad, with a two-finger swipe.
https://www.dropbox.com/s/7qgmg5dr1bxosqq/PageController3%203.zip?dl=0
It is pretty basic stuff, but I post it with the hope it can same someone else some time. Mark

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);

MapKit changes annotation view order when map is scrolled in XCode 4.6

I am an experienced iOS developer, but this has really stumped me. I am simultaneously submitting a problem report to Apple.
I'm adding annotations to a MKMapKit map (Xcode 4.6). Each annotation is a MyAnnotation class; MyAnnotation defines a property, location_id, that I use to keep track of that annotation.
The problem is simple: I want the MyAnnotation with a location_id of -1 to appear in front of everything else.
To do this, I am overriding mapView:didAddAnnotationViews: in my MKMapViewDelegate:
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
// Loop through any newly added views, arranging them (z-index)
for (MKAnnotationView* view in views) {
// Check the location ID
if([view.annotation isKindOfClass:[MyAnnotation class]] && [((MyAnnotation*)(view.annotation)).location_id intValue]==-1 ) {
// -1: Bring to front
[[view superview] bringSubviewToFront:view];
NSLog(#"to FRONT: %#",((MyAnnotation*)view.annotation).location_id);
} else {
// Something else: send to back
[[view superview] sendSubviewToBack:view];
NSLog(#"to BACK: %#",((MyAnnotation*)view.annotation).location_id);
}
}
}
this works just fine. I have an "add" button that adds an annotation to a random location near the center of my map. Each time I push the "add" button, a new annotation appears; but nothing hides the annotation with the location_id of -1.
** UNTIL ** I scroll!
As soon as I start scrolling, all of my annotations are rearranged (z-order) and my careful stacking no longer applies.
The really confusing thing is, I've done icon order stacking before with no problem whatsoever. I've created a brand new, single view app to test this problem out; sending MKAnnotationView items to the back or front only works until you scroll. There is nothing else in this skeleton app except the code described above. I'm wondering if there is some kind of bug in the latest MapKit framework.
The original problem was in trying to add new annotations when the user scrolled (mapView:regionDidChangeAnimated:). The annotation adds; the mapView:didAddAnnotationViews: code fires; and then the order is scrambled by an unseen hand in the framework (presumably as the scroll completes).
In case you're interested, here is my viewForAnnotation:
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
// Is this an A91Location?
if([annotation isKindOfClass:[MyAnnotation class]]){
MyAnnotation* ann=(MyAnnotation*)annotation;
NSLog(#"viewForAnnotation with A91Location ID %#",ann.location_id);
if([ann.location_id intValue]==-1){
// If the ID is -1, use a green pin
MKPinAnnotationView* green_pin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
green_pin.pinColor=MKPinAnnotationColorGreen;
green_pin.enabled=NO;
return green_pin;
} else {
// Otherwise, use a default (red) pin
MKPinAnnotationView* red_pin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
red_pin.enabled=NO;
return red_pin;
}
}
// Everything else
return nil;
}
And my class:
#interface MyAnnotation : NSObject <MKAnnotation>
#property (strong, nonatomic, readonly) NSNumber* location_id;
#property (strong, nonatomic, readonly) NSString* name;
#property (strong, nonatomic, readonly) NSString* description;
#property (nonatomic) CLLocationCoordinate2D coordinate;
-(id) initWithID:(NSNumber*)location_id name: (NSString*) name description:(NSString*) description location:(CLLocationCoordinate2D) location;
// For MKAnnotation protocol... return name and description, respectively
-(NSString*)title;
-(NSString*)subtitle;
#end
Could you possibly try the following as a work around:
1) Remove your code in mapView:didAddAnnotationViews:
2) Pickup when the map is moved or pinched (I assume this is what you consider to be a scroll right?) - I do this with gestures rather than the mapView regionChanged as I have had bad experiences such as unexplained behaviour with the latter.
//recognise the paning gesture to fire the didDragMap method
UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(didDragMap:)];
[panRec setDelegate:self];
[self.mapView addGestureRecognizer:panRec];
//recognise the pinching gesture to fire the didPinchMap method
UIPinchGestureRecognizer *pinchRec = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(didPinchMap:)];
[pinchRec setDelegate:self];
//[pinchRec setDelaysTouchesBegan:YES];
[self.mapView addGestureRecognizer:pinchRec];
//recognise the doubleTap
UITapGestureRecognizer *doubleTapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didPinchMap:)];
[doubleTapRec setDelegate:self];
doubleTapRec.numberOfTapsRequired = 2;
[self.mapView addGestureRecognizer:doubleTapRec];
3) write a custom "plotAnnotations" function to show your annotations. This function will loop through all your annotations and save them in an array "annotationsToShow" ordered by your location id DESC so that your location_id -1 are added last. Use [self.mapView addAnnotations:annotationsToShow]; to display them.
4) Call plotAnnotations in your gestureRecognizer functions
- (void)didDragMap:(UIGestureRecognizer*)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
[self plotAnnotations];
}
}
- (void)didPinchMap:(UIGestureRecognizer*)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
[self plotAnnotations];
}
}
5) You may need to delete all annotations before displaying the new ones in 3) [self.mapView removeAnnotations:annotationsToRemove];

Button Crashes App with (id) sender

I am attempting to make a basic game which requires a serious of buttons to control player movement. Keep in mind I am using cocos-2d. My goal is to have the buttons be holdable and move a sprite when held down. The code i am using now looks like this.
CCMenuItemHoldable.h
#interface CCMenuItemSpriteHoldable : CCMenuItemSprite {
bool buttonHeld;
}
#property (readonly, nonatomic) bool buttonHeld;
CCMenuItemHoldable.m
#implementation CCMenuItemSpriteHoldable
#synthesize buttonHeld;
-(void) selected
{
[super selected];
buttonHeld = true;
[self setOpacity:128];
}
-(void) unselected
{
[super unselected];
buttonHeld = false;
[self setOpacity:64];
}
#end
and for the set up of the buttons
rightBtn = [CCMenuItemSpriteHoldable itemFromNormalSprite:[CCSprite spriteWithFile:#"art/hud/right.png"] selectedSprite:[CCSprite spriteWithFile:#"art/hud/right.png"] target:self selector:#selector(rightButtonPressed)];
CCMenu *directionalMenu = [CCMenu menuWithItems:leftBtn, rightBtn, nil];
[directionalMenu alignItemsHorizontallyWithPadding:0];
[directionalMenu setPosition:ccp(110,48)];
[self addChild:directionalMenu];
This all seems to work fine but when i do
-(void)rightButtonPressed:(id) sender
{
if([sender buttonHeld])
targetX = 10;
else{
targetX = 0;
}
}
The crash has been fixed but I am trying to get my sprite to move. In my game tick function I add the value of targetX to the position of the sprite on a timer, still no movement.
Please, always include a crash log when asking questions about crashes.
In your case, I can guess the problem. You are adding this selector:
#selector(rightButtonPressed)
Your method is called
rightButtonPressed:(id)sender
Which would be, as a selector, rightButtonPressed: - note the colon indicating that an argument is passed. Either change the method so it has no argument, or add a colon to the selector when you create the button.
The crash log would be telling you this - it would say "unrecognised selector sent to..." with the name of the receiving class, and the name of the selector.

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