Blur for different UIImages in same UITableViewCell - ios

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

Related

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

scrollView with UIImage respond touch

I'm using a simple code to add some images to my UIScrollView. Also I've implemented another code to detect touches on each image.
Here is the code:
(void)handleSingleTap:(UIGestureRecognizer *)sender
{
int senderTagIs;
senderTagIs = sender.view.tag;
if (sender.view.layer.borderColor != [UIColor cyanColor].CGColor) {
sender.view.layer.borderColor = [UIColor cyanColor].CGColor;
UIImageView *showFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+100];
[showFullImage setTag:sender.view.tag+200];
[self.view addSubview:showFullImage];
showFullImage.hidden = NO;
NSLog(#"Show tag is: %i", sender.view.tag);
}
else
{
sender.view.layer.borderColor = [UIColor whiteColor].CGColor;
UIImageView *hideFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+200];
[hideFullImage setTag:sender.view.tag+100];
hideFullImage.hidden = YES;
NSLog(#"Hide tag is: %i", sender.view.tag);
}
}
The above code, sets the border color to cyan and show my small images from UIScrollView, in another UIImageView.
But my problem is, that I can't set the option to hide all images and set border color white for all images when one image is touched.
Ex: If I touch the first image, then the code will work, my big UIImageView will show touched image and touched image from UIScrollView will get the cyan color for border, so far so good.
Now, If I touch third image, my first image is shown, the color border is cyan, and so... I have to touch first image again to disable, but this is not what I want.
So, we've got a few things going on here. First, I'm assuming you're trying to show a collection of images in a scroll view with some custom padding to indicate selection around them. This sounds tailor make for using UICollectionView with a custom cell.
Absent further information, you're not resetting the old color. Either keep a reference to a selected image as a class variable or, assuming your image views are in a collection object like an NSArray, begin your method by iterating through the objects and resetting their view to an unselected state.
If you just need to hack together a solution, the second option should work. I really recommend using UICollectionView. It's a bit more work in the beginning, especially if you're not experienced with it, but it's well worth learning. Here's a good tutorial on UICollectionView.

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

Moving Background of a View

I have a really large image that I want to use as a background image of a view. However, I don't want to display the entire image at once; I want only a part of the image to be displayed, and then I want to animate it to display other parts of it, similar to the "infinite background" in games (only not infinite in my case ;)).
What is the best way to do this? Will I have to separate the image in several pieces and then somehow animate the transition between the pieces, or is there a better way?
How about having UIScrollView as a background view? You can then put UIImageView inside that scroll view and control scroll view's contentOffset as needed.
I found the solution. This piece of code does the magic:
self.backgroundImageView = [[UIImageView alloc] initWithFrame: self.view.bounds];
self.backgroundImageView.image = [UIImage imageNamed:#"background.png"];
self.backgroundImageView.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview: self.backgroundImageView];
The key was setting the contentMode to UIViewContentModeBottomLeft.

Restore default UITableViewCell backgroundView

In short, I need to restore the default backgroundView for a UITableViewStyleGrouped cell.
cell.backgroundView = ??;
The long story:
I have a grouped tableview with some cells. For every cell the user needs to select some value or do some input. After that he can proceed to the next ViewController by touching a "next" button.
Anyway, to indicate to the user that he missed something, I display a nice red border around the cell with the wrong/missing user input. I do that by setting the backgroudView of the cell with a custom image like this:
cell.backgroundView = myErrorIndicatingImageView;
The cell now looks like this (screenshot, ignore the stars and label)
So far so good, this works like a charm. But after the user corrects the input I want to remove my red border background image and just show the original background again. The original background looks like this (screenshot):
And this seems to be a problem.
I tried several things
// try by setting the background to nil
cell.backgroundView = nil;
this removes the background completely and I'm lost with a cell without background.
// try it with addSubview and later remove the subview again
[cell.backgroundView addSubview:myErrorIndicatingImageView];
this does nothing. The myErrorIndicatingImageView is not visible. Even a [cell.backgroundView bringSubviewToFront:myErrorIndicatingImageView] does not help.
Right now the only solution I have is to create a new UITableViewCell for every single call to cellForRowAtIndexPath. This works but is just bad code and ignores completely the technique to reuse cells.
There must be a simpler solution...something like
cell.backgroundView = [self.tableView getDefaultBackgroundView];
what about trying this:
cell.backgroundView = [[UIView alloc] initWithFrame:cell.backgroundView.frame];
Or you can make the background view as a UIImageView and set 2 images in problem and fixed
Assuming you are doing this with a custom table cell class, save the original backgroundView in some ivar, then apply the new background. When you want to reset the background, set the backgroundView back to the original, saved background view.

Resources