Passing a UIImage from one view controller to a UITableViewCell - ios

I am using a UITable to show images (Like a feed in instagram) I have the image saved in the viewcontroller I am taking the picture, but I need a way to pass the UIImage i have created to the UITableViewCell. What would be the best way to accomplish this.

You need to subclass UITableViewCell and give it a public property like
#property (nonatomic, strong) UIImage *thumbnailImage;
then in your uitableviewcells implementation file create and add a UIImageView however you want.
- (void)setThumbnailImage:(UIImage *)thumbnailImage
{
_thumbnailImage = thumbnailImage;
self.imageView.image = _thumbnailImage;
}
in your cellForRowAtIndexPath method, do something like
cell.thumbnailImage = [UIImage imageNamed:"YourImage.png"];
This is just one of many ways to do this.

Related

how to get image name when click on uiimageview?

I have a UIScrollview.
In the UIScrollview there are 4 UIImageViews with images.
When I click on any UIImageview I want to know the name of that image.
UIImageView just keeps the pointer to the place the UIImage is stored inside the memory, so it's not possible with UIImageView. You would need to create a subclass of UIImageView with a NSString variable which stores the value to access it later. Something like this:
#import <UIKit/UIKit.h>
#interface MyImageView : UIImageView
#property (strong, nonatomic) NSString *imageName;
#end
The image names are not stored in UIImage objects so you have to keep them somewhere else for lookup. To detect touches in image views you either have to subclass them and add code for touch detection, add tap recognizers or replace them with buttons.

Subclass from UIView or UIImageView? Overthinking a simple image downloading task.

This question is two fold:
1) Understanding which subclass method is better when creating an Asynchronous download ImageView class
2) And how to use this class in a UITableViewController so that the class view is automatically adopted in a UITableViewCell.
Task at hand
I just received a fun little task from someone but I'm overthinking the solution.
Question 1 - Which method is better for my purposes?
Keeping in mind that I will have to reuse the class to display some images perhaps in a UITableViewController, I thought about two possible ways of going about implementing such a class:
1) Either create a class that subclasses from UIView, then add the required imageURL property, and then also add a UIImageView property container to this subclass which the image will download into once the asychronous request has been downloaded once the user set the URL property when he is to "re-use" the class in a table view controller.
Or
2) Create a class subclassing from UIImageView and then only add that one required property imageURL which would then save me the mess of having to progammatically create an image container.
Question 2: Whats the best way to re-use this Asynchronous ImageView class that I have created in a UITableViewController.
The problem (at least what I believe may be a stumbling block) is when I create the table view controller, whats the best way to have this image set the image property of a UITableView cell?
something like
AsynchronousImageViewDownloader *myImageView = [[AsynchronousImageViewDownloader alloc] init];
[myImageView setImageURL:[NSURL urlWithString:#"url.com/image.jpg"]];
//At this point im not sure how to have the image display in the image property below.
//Remember that the image should automatically show in the view when the url has been set.
cell.image = ??
How would I go about doing something like this?
Create a subclass of UITableViewCell, and add a public property to that class for your AsynchronousImageViewDownloader imageView. In init, create the imageview (it will not have a URL at this time) and add it to self.contentView.
Then in cellForRow you'll be able to do something like
cell.customImageView.url = #"someURL"
Create UIImageView subclass with a single new property, imageURL. In the subclass's implementation, override setImageURL with something similar to the following:
- (void)setImageURL:(NSURL *)imageURL
{
if (!_imageURL) {
_imageURL = imageURL;
// Do asynchronous image download, setting the self.image property, on the main thread, in the completion block.
}
return _imageURL;
}

UIImageView.image setter

I have two side view made this way:
containerView - container of both views
firstView - visible view which is made of UIImageView
secondView - view visible after flipping
Now I'm downloading image async to present on firstView using SDWebImage library with method:
[view.imageView setImageWithURL:[NSURL URLWithString:item.image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Image can be horizontal or vertical.
Now I want secondView to be the same size as presented image. I was thinking of setting it in UIImageView custom setter but I don't know how to get that method.
Maybe any other ideas?
edit:
Image
I think I know how to do it. What I just need now is actual VISIBLE size of UIImageView.image.
If you're happy to import AVFoundation to your project these is a convenience function AVMakeRectWithAspectRatioInsideRect which takes an image size and image view frame and gives you the size at which the image will be displayed.
You can obviously write some code yourself to calculate the same values using a little maths.
Create a property for the image that is being downloaded. Then create a custom setter for this property - in here update your secondImageView size.
This way, each time the imageBeingDownloaded has finished downloading - it will set the image - which will then update the secondImageView size
i.e.
In the implementation file
#property (nonatomic, weak) UIImage *imageBeingDownloaded; // creates the property
// The custom setter
- (void)setImageBeingDownloaded:(UIImage *)imageBeingDownloaded {
_imageBeingDownloaded = imageBeingDownloaded;
self.secondImageView.size = _imageBeingDownloaded.size; // each time the image is set this will update secondImageView
}
Then
[view.imageView setImageWithURL:[NSURL URLWithString:item.image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
becomes
[self.imageBeingDownloaded setImageWithURL:[NSURL URLWithString:item.image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

Styling Custom UITableViewCell Using UIAppearance

I have a custom UITableViewCell and I want to style that using the UIAppearance protocol.
#interface MyCell : UITableViewCell<UIAppearance>
{
}
#property (nonatomic,weak) UIView *backgroundCellView UI_APPEARANCE_SELECTOR;
MyCell.m:
-(void) setBackgroundCellView:(UIView *)backgroundView
{
NSLog(#"setBackgroundCellView");
[super setBackgroundView:backgroundView];
}
In the app delegate I am doing the following:
[[MyCell appearance] setBackgroundCellView:[UIImageView .... with an image]];
It assign the background to few cells but not all the cells. Usually the assigned ones are in the middle.
One cannot assigning the same UIImageView to multiple cells and try to display that. The same UIView cannot be part of only one subview hierarchy.
What needs to be done instead is to make is a custom background view which has a UIImage configurable with UIAppearance.
Basically one should configure the background view's image using UIAppereance and not the background view itself.

How to insert a UITextView to a UIImageView

I'm programming an application, in wich you create UIImageView-objects.
The color of these objects can be set.
But the next step I am trying to do is: set a TextView in the imageView, but this text should entered by the user (certainly realized with the class UIKeyboard.)
Im sum: I have a imageView, which needs an editable UITextView in it.
Do you have any idea how it could be realized?
Your approach is wrong.
You want an image view because the app lets you "create an image"
In reality and technically this isn't correct, what you will want to be doing is using a simple UIView to set he colour of an area, and you an place a UITextField or UITextView over that.
A UIImageView is a very specific UIView subclass for displaying images. You can't add your other UI inside of it, the best you could do is over it.
I you do use a UIView, when you're done you need to "convert" your UIView to an image which you can then use to save, send etc
For more about that check out Save UIView's representation to file
You simply want to subclass UIImageView and add a UITextView during init. Like this
#interface MyImageView : UIImageView {
}
#property (nonatomic, strong) UITextView *textView;
#end
And the implementation
#implementation MyImageView
- (id)init {
if ((self = [super init])) {
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(...)];
[self addSubview:self.textView];
}
return self;
}
#end

Resources