UIImageView.image setter - ios

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"]];

Related

Blur for different UIImages in same UITableViewCell

So in my custom UITableViewCell I have 4 different UIImageView's & for each UILongTapGestureRecognizer.
Table View Cells before adding images
After I add images and table looks like this :
Table View Cells after adding images
I need to be able to delete image after long tap gesture recognizer, when the chosen image blurs out and remove button is shown.
I've added the long press gesture recognizer method and while I understand that I need to apply blur to the chosen image and not the UIImageView itself, I dont seem to get how to pass the reference of the picked image to the cell so that I can apply the blur filter to the image.
Is delegate legitimate to use in this case?
In order to apply blur effect as you mentioned you need the image itself.
You can try something like this :)
UIImage *image = [yourImageView image];
UIImage *blurredImage = [UIImageEffects imageByApplyingBlurToImage:image withRadius:30 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
yourImageView.image = blurredImage;
You can get UIImageEffects apple file from here :)
https://developer.apple.com/library/ios/samplecode/UIImageEffects/Listings/UIImageEffects_main_m.html
Hope it helps :)

UIImage view does not display the image

I'm having a few issues with displaying a UIImage in an app that I'm developing. The steps I took to do this are as follows:
1 - Add a UIImageView to my main view controller in the storyboard. I did this using the interface builder.
2 - I then created an IBOutlet for the UIImageView in the viewcontroller.h. I ensured proper referencing of the image view from the outlet.
3 - I set the image for the UIImageView like so:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flame_icon.png"]];
This was done within the viewDidLoad method in the viewcontroller.m file.
Troubleshooting:
I checked whether the _fireIcon variable is being set. It is.
I checked whether the viewDidLoad method is being called. It is.
In addition, the image is displayed when I statically set it via the storyboard itself. But when I do it via code, it does not display.
Let me know if you need any more additional information, or code snippets.
you need not to initialize an imageview as you are using Interface builder for image view
_fireIcon.image = [UIImage imageNamed:#"flame_icon.png"]; // or [_fireIcon setImage:[UIImage imageNamed:#"flame_icon.png"]];
This sets the image.
When we drop the UI elements using Interface builder , we need not initialize them, as they are taken care by apple.
Doing this you reset your property with newly created UIImageView:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flame_icon.png"]];
And your reference to UIImageView from storyboard gets compeletely lost.
Instead you want to set image:
_fireIcon.image = [UIImage imageNamed:#"flame_icon.png"];
Firstly, you do need to allocate the Element created via Interface builder, you just need to set the image to UIImageView directly like this :-
[myImage setImage:[UIImage imageNamed:#"5.jpg"]];
Make sure image name is correct and its extension too. I just tried and its working great here. Let me know if you still face problem here.
Swift 3.0
statusImageView?.image = UIImage(named: "select.png")

Passing a UIImage from one view controller to a UITableViewCell

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.

Update UIImageView in a UITableView cell with transparency

I have an imageview class that subclasses the UIImageView class, it is being displayed in an UITableView that dequeueReusableCellWithIdentifier.
#interface ImageView : UIImageView {
.
.
.
when it is time to update the image from one image to another, the update itself works
[self setImage:[UIImage imageWithContentsOfFile:[self resourcesDir:url]]];
but my images are transparent PNG (with the same dimensions) so part of the pervious image is visible under the updated one (since the cells themselves are reused)
I tried various ways to make the imageView clear itself first, but none work, this is what I tried:
self.image = nil;
[self setBackgroundColor:[UIColor clearColor]];
[self setNeedsDisplay];
[self performSelectorOnMainThread:#selector(setNeedsDisplay) withObject:nil waitUntilDone:NO];
Any Idea ?
Be sure you are NOT using a new instance of the UIImageView. It must be the same instance for your code to work properly. You idea sounds like it should work properly.

UIView::addSubView obstructs the navigation bar originally at the top

I designed a very simple interface for an ipad device: UIView + a navigation bar.
Then after the view is load, it will download an image from a location and use the following method to display it:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
UIImage* testImg = [UIImage imageWithData:_networkData];
UIImageView* testView = [[UIImageView alloc] initWithImage:testImg];
[_view addSubview:testView];
[testView release];
}
The problem is now the new UIImage occupies the whole visible area of the Ipad.
I wonder how can I fix this issue? I suppose I have to find out the frame of the display area in the original view, and then assign it to the UIImageView instance?
initWithImage will automatically adjust the frame to match the size of the image you're passing in. To tell the UIImageView to take the same size as its parent view you could just add the following line before you add the subview:
testView.frame = _view.bounds
...we use bounds rather than frame because the superview may have an offset that we don't want the image view to have.

Resources