Method doesn't display UIImageView (iOS XCode Cocoa touch) - ios

I'd like to have a method display an image. My view controller calls the method:
eduPopup *thisEduPopup = [[eduPopup alloc] init];
[thisEduPopup displayPopup];
In the method I have:
-(void) displayPopup {
UIImageView *thisImage = [[UIImageView alloc] init];
thisImage.image = [UIImage imageNamed:#"referral_eduscreen_text_solid.png"];
thisImage.frame = CGRectMake(0, 0, thisImage.image.size.width, thisImage.image.size.height);
[self.superview addSubview:thisImage];
}
...but I get nothing. Is self.superview the right reference? What I should be putting there?

Related

Each image for left callout view

I want leftCalloutAccessoryView have a location's own image, I know how to make a leftCalloutAccessoryView on callout bubble, however I couldn't get the image path
location information is init form a SpotAnnotation class, like bellow
- (id) initWithTitle:(NSString *)aTitle latitude:(CLLocationDegrees)aLat longitude:(CLLocationDegrees)aLon imageName:(UIImage *)imgName{
self.coordinate = CLLocationCoordinate2DMake(aLat, aLon);
self.currentTitle = aTitle;
self.places = [[NSMutableArray alloc] initWithCapacity:0];
self.image = imgName;
return self; }
In ViewController works like this
- (NSArray *) spots{
SpotAnnotation *s1 = [[SpotAnnotation alloc] initWithTitle:#"TaipeiZoo" latitude:25.039550 longitude:121.560265 imageName:[UIImage imageNamed:#"taipeiZoo.jpg"]];
SpotAnnotation *s2 = [[SpotAnnotation alloc] initWithTitle:#"Eslite" latitude:25.039720 longitude:121.565874 imageName:[UIImage imageNamed:#"Eslite.jpg"]]; }
biggest question is here -> initWithImage:
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:"how can I get image path here"];
leftIconView.frame = CGRectMake(10, 10, 50, 50);
pin.leftCalloutAccessoryView = leftIconView;
ummm... I solved it..
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:((SpotAnnotation *)annotation).image];
leftIconView.frame = CGRectMake(0, 0, 50, 50);
leftIconView.contentMode = UIViewContentModeScaleAspectFit;
pin.leftCalloutAccessoryView = leftIconView;
problem is initWithImage:((SpotAnnotation *)annotation).image
I will try hard to get it understand!

Resets View when dismissing from JTSImageViewController

I have a UITableView with images. When i scroll down and select an image, Onclick of image i am using following library to make it zoomIn/ZoomOut/Panning etc. When i come back, the tableview again scroll back starting at 0 element. I am not using didSelectRow but a custom selector. Has anyone worked with this library before? I can post the code if required.
https://github.com/jaredsinclair/JTSImageViewController
Assigning Selector
AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 300.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] init];
[tapRecognizer addTarget:self action:#selector(bigButtonTapped:)];
[imageView addGestureRecognizer:tapRecognizer];
Method Call
- (void)bigButtonTapped:(id)sender {
UIView *view = [sender view];
AsyncImageView *imageView1 = (AsyncImageView *)[view viewWithTag:IMAGE_VIEW_TAG];
// Create image info
JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
#if TRY_AN_ANIMATED_GIF == 1
#else
imageInfo.image = imageView1.image;
#endif
imageInfo.referenceRect = imageView1.frame;
imageInfo.referenceView = imageView1.superview;
imageInfo.referenceContentMode = imageView1.contentMode;
imageInfo.referenceCornerRadius = imageView1.layer.cornerRadius;
// Setup view controller
JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
initWithImageInfo:imageInfo
mode:JTSImageViewControllerMode_Image
backgroundStyle:JTSImageViewControllerBackgroundOption_Blurred];
// Present the view controller.
[imageViewer showFromViewController:self transition:JTSImageViewControllerTransition_FromOriginalPosition];
}
It seems JSTImageViewController doesnt play nice with UITableView in iOS8. This issue has been reported in the GitHub page for JSTImageViewController. There's also a possible workaround mentioned :
...Apparently it's the default behaviour to reset the contentOffset after returning from a modal view controller...
...setting the modal presentation style of JTSImageViewController to UIModalPresentationOverFullScreen fixes the issue (since it doesn't remove the hierarchy below it and retains the scroll position I think).
if ([[[UIDevice currentDevice] systemVersion] compare:#"8.0" options:NSNumericSearch] != NSOrderedAscending)
{
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
}
Read the full thread Here

ScrollView image addSubview issue

I'm trying to add an imageView in the ScrollView but I always get the ScrollView empty. Please where would be my issue? While I have added the delegate method <UIScrollViewDelegate> also made the connection in the storyboard.
My code:
- (void) viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void) slider {
_scrlView = [[UIScrollView alloc]init];
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
_img3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
First, you don't add the scrollView to any view, so it won't be presented on screen.
Second, this:
_scrlView = [[UIScrollView alloc]init];
creates a scrollView with CGRectZero which is the same like writing:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,0,0)];
This means the contentSize is also CGRectZero.
Edit: If you don't want to override the creation of the scrollView (because you have already set it in Interface Builder), remove the line _scrlView = [[UIScrollView alloc]init];.
But if you are using Auto Layout in the storyboard you should call [self slider] in viewDidLayoutSubviews.
You need to set frame first, for getting its width and height,
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(YOUR_X, YOUR_X, YOUR_WIDTH, YOUR_HIEGHT)];
// do this if you have not added Scrollview in storyboard, if Added, then no need to invoke, init by line [[UIScrollView alloc] init] ;
...
...
[self.view addSubview:_scrlView];
}
I would suggest one more thing, to call your slider method from viewDidAppear method of UIView
Enjoy Coding !!
You need to allocate the ScrollView if have not used a storyboard. Also in that case, specify a frame:
_scrlView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
You are reallocating the imageviews, try to set image after allocating it once.
Another thing : Dont init scrollview again.
- (void)viewDidLoad {
[super viewDidLoad];
[self slider];
}
-(void)slider{
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = YES;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = false;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 3, _scrlView.frame.size.height);
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*0),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*1),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
_img3 = [[UIImageView alloc] initWithFrame:CGRectMake((_scrlView.frame.size.width*2),0,_scrlView.frame.size.width,_scrlView.frame.size.height)];
[_img1 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img2 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_img3 setImage:[UIImage imageNamed:#"1234.jpg"]];
[_scrlView addSubview:_img1];
[_scrlView addSubview:_img2];
[_scrlView addSubview:_img3];
}
My reply would be
if you are not using Storyboard or Interface builder , also autolayout is unchecked, set the frame of uiscrollview after its allocation and use alloc init only one time for one image. Add image on scrollview and scrollview on superview
if you are not using Storyboard or Interface builder and autolayout is checked, then put proper constraints on scrollview and imageviews. Read this
https://developer.apple.com/library/ios/technotes/tn2154/_index.html

UITapGestureRecognizer EXC_BAD_ACCESS in self-contained UIView subclass

I've used UITapGestureRecognizer tons of times, but in this case I'm getting EXC_BAD_ACCESS when a tap occurs. I think it has to do with the fact that I'm adding it to an Alert-type overlay view. And for some reason the overlay view isn't getting retained even though it's on-screen.
I'm creating the view like this:
HelperView *testHelper = [HelperView helperWithBodyText:#"test text"];
[testHelper presentAtPoint:screenCenter];
The convenience method in HelperView.m looks like this:
+ (id)helperWithBodyText:(NSString*)text
{
return [[self alloc] initWithFrame:CGRectZero bodyText:text];
}
And the rest of the code looks like this:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.container = [[AGWindowView alloc] initAndAddToKeyWindow];
self.container.supportedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.overlay.backgroundColor = [UIColor redColor];
self.overlay.alpha = 0.6;
[self.container addSubviewAndFillBounds:self.overlay]; //this fills the screen with a transparent red color, for testing
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissMe:)];
[self.overlay addGestureRecognizer:tap]; //I want to dismiss the whole view if the overlay is tapped
self.content = [[UIView alloc] initWithFrame:CGRectZero];
}
return self;
}
- (id)initWithFrame:(CGRect)frame bodyText:(NSString*)bodyText
{
self = [self initWithFrame:frame];
if (self) {
//TEST frame
self.content.frame = CGRectMake(0, 0, 217, 134);
// Initialization code
UIImage *bgImage = [[UIImage imageNamed:#"helper-bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(30, 28, 20, 20)];
UIImageView *bgImgView = [[UIImageView alloc] initWithImage:bgImage];
bgImgView.bounds = self.content.frame;
[self.content addSubview:bgImgView];
}
return self;
}
- (void)presentAtPoint:(CGPoint)loc
{
CGPoint newPoint = [self.container convertPoint:loc toView:self.container];
self.content.center = newPoint;
[self.container addSubview:self.content];
}
- (void)dismissMe:(UITapGestureRecognizer*)recognizer
{
//this never happens - I get EXC_BAD_ACCESS when I tap the overlay
}
HelperView is not the view getting displayed and it's not getting retained, but you're using as the target for the gesture recognizer. The AGWindowView property "container" is getting displayed and retained by its superview. Your code needs refactoring since you have this view HelperView that doesn't ever display anything itself, but if you want it to work like this you need to retain HelperView so it doesn't automatically get released. You can do this by assigning it to a strong instance variable.

Can't access UIImageView if created programmatically

If I create a UIImageView via Storyboard and add it as a #property into a ViewController.h, I can access that UIImageView from anywhere in the ViewController.m via [self UIImageView] or self.UIImageView.
But If I create the UIImageView programmatically from viewDidLoad as you see below, I seem to lose the ability to access it from outside viewDidLoad.
UIImageView *prevImgView = [[UIImageView alloc] init];
UIImageView *currImgView = [[UIImageView alloc] init];
UIImageView *nextImgView = [[UIImageView alloc] init];
NSArray *imageViews = [NSArray arrayWithObjects:prevImgView, currImgView, nextImgView, nil];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview: scrollView];
CGRect cRect = scrollView.bounds;
UIImageView *cView;
for (int i=0; i<imageViews.count; i++) {
cView = [imageViews objectAtIndex:i];
cView.frame = cRect;
[scrollView addSubview:cView];
cRect.origin.x += cRect.size.width;
}
So later on if I want to modify the image that is being displayed in any one of the UIImageView I've created from viewDidLoad, I can't seem to access it. Does anyone know what I'm doing wrong?
You would need to create your UIImageView in your header file and then it would be available throughout the rest of the view. You will need to add it to the view in viewDidLoad though.
Header
UIImageView *image;
Main // ViewDidLoad
image = [UIImageView alloc] .....
[self.view addSubView image];
Main Function .....
[image setImage ....
The answer AgnosticDev is trying to say here is to add an ivar to your view controller's .h file... e.g.:
#implementation YHLViewController : ViewController
{
UIImageView * currImageView;
}
And then in your "viewDidLoad" method, you can allocate and initialize it via:
currImageView = [[UIImageView alloc] init];
and have access to it from anywhere else in your view controller.

Resources