UISlider to change UIImage brightness/contrast in Swift Playground is very laggy. - ios

I am currently creating a filter app. The user has the option to change brightness, contrast, etc.
I have implemented the ability to change these attributes of the image and it currently works but when I test this in Xcode Playgrounds the slider is extremely slow and makes Xcode very laggy.
I assume this is because I have written this in a very inefficient way. I don't want to copy and paste all of my code to Stackoverflow (I want to avoid making this too complicated) so I have uploaded it on Github. Located Here if you download the repo and open the playground up it is 100% running and if you test the slider you will see it is very laggy.
I think what is making it so laggy is that I am reseting the image view every time the slider value is changed resulting in a new UIImage being assigned to it every fraction of a second. Here is a tiny snippet of what I I just said, in code. You will probably still have to look at the code I posted on github since I made some protocols and classes
slider.addTarget(c, action: #selector(c.BrightnessChanged(sender:)), for: .valueChanged)
func updateBrightness(sender:UISlider) {
controls.brightness(sender.value)
img.image = controls.outputUIImage()
}
I genuinely have no intention asking for a solution without having tried myself; I have searched all over the interwebs to figure this out with no success.
Thanks Stackoverflow homies!

Related

UICollectionView & AVPlayer Performance

I have a UICollectionView whose cells each have an AVPlayerLayer, all playing a video at the same time. About 9 cells fit on the screen at a time and it's pretty laggy so I need ways to boost the performance to achieve smooth scroll. There's a few things I've tried already:
1) Each cell has only one instance of AVPlayer. It doesn't get created, instead, player.replaceCurrentItemWithPlayer is called when the video url changes.
2) Because I'm using ReactiveCocoa, it's trivial to skip repeat urls to avoid playing the same video twice.
What else can I do to speed up the scroll and performance?
First I want to say it's a bit crazy to see several players in action at the same time: it's a heavy task of rendering anyway.
As far as I know, simply scaling/re-framing the video to smaller size doesn't make it less content to render: if you have a 100x100 picture to render and you render it in a frame of 10x10, it still consumes the same amount of memory as the original picture would consume; same thing goes for videos. So, try to make your video assets having similar resolution as the frame where you would present them.
Store each cell in an NSCache or NSMapTable using the NSIndexPath as the key for each; then, whenever you need a cell, call one directly from the cache or map table.
Obviously, you'll have to create all the cells at once, but you'll get the same scrolling performance as you do with nothing in the cells at all.
Need sample code?
Here's my latest iteration of a perfectly smooth-scrolling collection view with real-time video previews (up to 16 at a time):
https://youtu.be/7QlaO7WxjGg
It even uses a cover flow custom layout and "reflection" view that mirrors the video preview perfectly. The source code is here:
http://www.mediafire.com/download/ivecygnlhqxwynr/VideoWallCollectionView.zip
Unfortunately, UIKit will hit bottlenecks and drop frames when put under pressure for this use case. If you can muster refactoring code to use Facebook's AsyncDisplayKit / then this would be your best bet.
For objective-c / this project here is a good reference.
https://github.com/TextureGroup/Texture/tree/master/examples/ASDKTube
- (ASCellNode *)tableNode:(ASTableNode *)tableNode nodeForRowAtIndexPath:(NSIndexPath *)indexPath
{
VideoModel *videoObject = [_videoFeedData objectAtIndex:indexPath.row];
VideoContentCell *cellNode = [[VideoContentCell alloc] initWithVideoObject:videoObject];
return cellNode;
}
there is swift code available if you dig deep enough in github.

IOS GIF cocoapod that allows variable durations and loop counts

Im looking for a cocoapod that enables me to use an animation (gif file) that is upto 200 frames, in an efficient manner
Further it needs to be able to:
start/stop animation
set a duration that the animation should run for
Set the number of iterations the animation should run for
So far I have tried FLAnimatedImage and YLGIFImage , and neither appear to satisfy the above constraints while both are excellent for running through gifs in an efficient manner
I was able to use FLAnimatedImage to handle my requirements. See below
start/stop animation: handled by implementing suggestions outlined in comments here: https://github.com/Flipboard/FLAnimatedImage/issues/52
set a duration that the animation should run for: fixed by using an online gif editor
Set the number of iterations the animation should run for : Handled by using https://github.com/Flipboard/FLAnimatedImage/pull/60

Rendering UIView in Swift with #IBDesignable, crash

I have a graph built in Paintcode that I am showing in a UIView. It works fine in the App but will not render in storyboard. I have read a host of different posts about overriding init frame, and init coder. However, I can't find a specific example that helps me with my problem, by showing the practical use of these overrides.
The graph takes 14 variables to produce a bar chart. Here is the class code
import UIKit
#IBDesignable
class graphRisk: UIView {
override func drawRect(rect: CGRect) {
StyleKitGraph.drawRiskGraph(ehsScale: ehsScale, ciScale: ciScale, stratScale: stratScale, qaScale: qaScale, delScale: delScale, leadScale: leadScale, peopScale: peopScale, hrmScale: hrmScale, pmScale: pmScale, costScale: costScale, finScale: finScale, assScale: assScale, itsScale: itsScale, engScale: engScale)
}
}
It looks good in the App, and it is no big deal if it doesn't render as the variables are coded, and the design is fine, however, I removed the #IBDesignable tag and it still errors the same.
Finally, how do I get the UIView to update when I enter the view controller it resided in?
So the questions are;
1 How do I get render to work for this code example?
2 Is there a way to not render the view, and stop the error that way?
3 Do I need to worry about the error as the App runs fine?
4 How do I get the UIView to refresh. What is the syntax for .setNeedsDisplay() etc? I tried lots of things and none updated or reloaded or refreshed.
As always I truly welcome all help, and thank you.
Not sure what version of Xcode you are using. I am using Xcode 6.4.
I, like you, have researched for days on many websites and tried many solutions. None of them worked. At last, I removed the #IBDesignable to my custom UI class. Not only did the error went away, the instance of the custom UI class previewed fine in Main.storyboard.
Perhaps, in the new development of Xcode, #IBDesignable is no longer necessary.

Adding array of UIImage in a UIViewController with some alignment

I just started my first project in iOS, recently I have encountered a problem in the UI when i receive it from the designer.
The expected output is shown as below and i can do most of it without any problem.
Now the problem came when I am about to implement this :
As the number of colour varies depending on item, I cannot add it like what I did for the share and love button I have no idea how can i implement something like this, I have look through the Object Library in the storyboard and cannot find any that can produce the output as expected .Any helps and guides are much apprecited!

Suspend redrawing while changing multiple children

I have a UIViewController that after asynchronously loading some data needs to resize some child views (Some UITableViews, and some UIScrollViews). This works just fine in the simulator, but on an actual device, it hangs for a long time (as much as 30 seconds in one case). I think the problem is that it wants to recalculate everything after each changed to the child view and I would like to be able to tell it to defer any recalculations until I've resized everything, but I'm drawing a blank on finding a way to do that. Is there a mechanism to do this?
So what I'm doing is something like this (I'm using C# with Xamarin but input in Obj-C would also be appreciated!):
// Need to suspend layout here...
MyTable.Frame = new RectangleF(...);
SomeOtherTable.Frame = new RectangleF(...);
ScrollView.ContentSize = new SizeF(...);
AnotherScrollView.ContentSize = new SizeF(...);
AnotherScrollView.Frame = new RectangleF(...);
// Ok - now you can redraw!
Update: Jacob's suggestion of disabling animation didn't seem to help, but I think I've isolated the problem to the table resizing. They seem to have the biggest impact on performance, but I'm not sure why, or how to mitigate the problem. I may follow up with a separate question on that.

Resources