ios How to add UIImage to UITextView asynchronous? - ios

I can add image asynchronously seperately by using UIImageView+AFNetworking
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
I can also add image to UITextView by
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImage:[UIImage imageNamed:#"image.png"]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
But I cannot add image asynchronously in UITextView like
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
So how can I add image asynchronously in UITextView?

Use a dispatch :
dispatch_async(dispatch_get_main_queue(), ^{
// async UI update here
});

Related

adding UIImageview in titleview of UINavigationBar not working as expected

So i am trying to add an UIImageview in titleview property of the UINavigationbar using the following code. The issue is that although i put specific size to the UIView that contains the UIImageview which contains the image it does not work as expected. The result is a bigger image in the Navigation bar than the one i set with the CGRectMake. Also in some views it does not align to the center! What am i missing? Is there another way to add the image to the navbar?
-(void)viewDidLayoutSubviews{
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,10.0f,16.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,10.0f,16.0f)];
CGSize imageSize = CGSizeMake(10, 16);
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);
imageView.frame = CGRectMake(marginX, 11, imageSize.width, imageSize.height);
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage sdk_imageName:#"logo.png"];
[imageView setImage:image];
[containerView addSubview:imageView];
self.navigationItem.titleView=imageView;
}
I reviewed your code and i found some frame issues and at the bottom you are not passing container view in titleview, that will not render the view as expected.
Please try this one.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
UIImageView *imageView = [[UIImageView alloc] init];
//containerView.backgroundColor = [UIColor redColor];
CGSize imageSize = CGSizeMake(60, self.navigationController.navigationBar.frame.size.height);
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);
imageView.frame = CGRectMake(marginX, 0, imageSize.width, imageSize.height);
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed: #"2.jpg"];
[imageView setImage:image];
[containerView addSubview:imageView];
self.navigationItem.titleView=containerView;
Result:
Much simpler approach... use auto-layout constraints for the image view size... centering is handled automatically.
10x16 image view as the titleView:
UIImage *img = [UIImage imageNamed:#"logo"];
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:img];
titleImageView.contentMode = UIViewContentModeScaleAspectFit;
titleImageView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:#[
[titleImageView.widthAnchor constraintEqualToConstant:10.0],
[titleImageView.heightAnchor constraintEqualToConstant:16.0],
]];
self.navigationItem.titleView = titleImageView;

How to make a UIImageView a button?

I have a UIImageView like so:
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
[self.view addSubview:imageView];
Couple of issues/questions:
How do I make this a clickable button?
I want to link to Settings (in my app) but do I have to create that image wheel like the settings icon that so many apps use or is it standard icon in iOS 7?
This is the icon I'm talking about:
Why doesn't my border show up?
1) If you want a button, you should use a UIButton and use [UIButton setImage: forState:]
2) Alternatively, you can add a UITapGestureRecognizer to your current UIImageView
here 2 choice u have to use your method
.h file
#import <QuartzCore/QuartzCore.h> //for radius and corner
1. UIButton
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
-(void) selectFav
{ //what ever you do like
}
2. UITap Gesture for UIImage View
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[imgView addGestureRecognizer:tapGesture1];
[self.view addSubview:imageView];
- (void) tapGesture: (id)sender
{
//handle Tap...
}
Try:
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
/* Here's the button Start */
UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height)];
theButton.backgroundColor = [UIColor clearColor];
theButton.showsTouchWhenHighlighted = YES;
[theButton addTarget:self action:#selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:theButton];
/* Here's the button End */
[self.view addSubview:imageView];
Your border:
0.1 means is 1/10th of a point - try 1.0 for a nice fine line
Also set:
imageView.clipsToBounds = YES;
1:if you have image view and want to clickable
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
2: Add gesture to image view like this, but don’t forget to setUserInteractionEnabled:YES
UITapGestureRecognizer *TapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(markerMove:)];
[TapGestureRecognizer setNumberOfTapsRequired:1];
[TapGestureRecognizer setNumberOfTouchesRequired:1];
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer: TapGestureRecognizer];

How to set specific size to UIImageView

I got a weird bug going on. I'm trying to programatically initialize an UIImageView with a specific frame and image. My image is 60x60, but I'm trying to resize it to 30x30 on the screen, which seems impossible.
The following works, but show the image in 60x60:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
// imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
And the following just doesn't show anything at all on the screen:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];
What's wrong?
EDIT: for those having the same problem, here is the working code:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
imgView.frame = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[imgView setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:imgView];
Start experiment with
imgView.center = CGPointMake(view.bounds.x/2, view.bounds.y/2);
imgView.bounds = view.bounds;
and see if this fills the superview. Then try CGPointMake(pointTouch.x, pointTouch.y) and CGPointMake(30,30) respectively.
I'm guessing this behaviour has something to do with warning about frame:
If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.
You could try initializing the UIImageView with a set frame, then setting the image after that:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 30, 30)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.image = [UIImage imageNamed:#"logo.png"];
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];

how to add a UILabel in UIImageView

i have big Image in my app. i create it dynamically and i want to add a UILabel on this image:
UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
UILabel *lblTitle = [[UILabel alloc] init];
lblTitle.text = #"SomeText";
lblTitle.frame = CGRectMake(xTitle, yTitle , titleWidth ,titleHeight);
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
[thumbnailImage addSubview:lblTitle];
but the [thumbnailImage addSubview:UILabel]; code does not work.
any one have any idea?
I want to add some text on bottom of picture like news article.
so I solve it with below code, if any one have a better idea please share it.
UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(xImage,imageHeight - titleHeight , titleWidth , titleHeight )];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xTitle, yTitle , titleWidth ,titleHeight)];
titleView.alpha = 0.3;
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
[self.view addSubview:thumbnailImage];
[self.view addSubview:titleView];
lblTitle.textLabel.text = #"SomeText";
first I use below line of Code:
[titleView addSubview:lblTitle]
but the alpha of titleView impact lblTitle and made it transparent. so after delete this line lblTitle Text appear clear and bright.
It's better to initialize the label with it's frame.
You should add a text to the label
You should add the UILabel object (lblTitle) to the imageview instead of the UILabel type.
With these corrections, your code should look like this:
UIImageView *thumbnailImage;
thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(xImage, yImage, imageWidth, imageHeight)];
[thumbnailImage setContentMode:UIViewContentModeScaleAspectFill];
thumbnailImage.clipsToBounds = YES;
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xTitle, yTitle , titleWidth ,titleHeight)];
lblTitle.textLabel.text = #"Your text";
[thumbnailImage addSubview:lblTitle];
thumbnailImage is a UIView, so you can add subviews to it.instead of writing the line
[thumbnailImage addSubview:UILabel];
edit it with the following ::
[thumbnailImage addSubview:lblTitle];
This will work . :)

Images are not properly displaying on UIScrollView

I have images in a UIScrollView, but the images are not displaying properly. The problem is that images are not at the same size. I want every image with the same size and height.
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
//CGRect workingFrame = _scrollView.frame;
CGRect workingFrame = CGRectMake(0, 0, 200, 134);
workingFrame.origin.x =0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
self.chosenImages = images;
NSLog(#"values=%#",_chosenImages);
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
If you set UIViewContentModeScaleAspectFit you will have the image with the maximum size of the UIImageView, but often smaller. If you want it to fill and have all the same size and keep aspect you have to set:
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setClipsToBounds:YES];

Resources