Skip to content

Commit

Permalink
Best Performances
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrePerrin committed Jul 25, 2017
1 parent 15f3d91 commit ce4cba8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
32 changes: 12 additions & 20 deletions PPMusicImageShadow/PPMusicImageShadow/PPMusicImageShadow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PPMusicImageShadow: UIView {

self.imageView?.contentMode = newValue
self.blurredImageView?.contentMode = newValue

}get{
return super.contentMode
}
Expand Down Expand Up @@ -46,7 +46,7 @@ class PPMusicImageShadow: UIView {
imageWidth = self.frame.size.width;
imageHeight = self.frame.size.height;
}

return CGSize(width: imageWidth, height: imageHeight)
}

Expand Down Expand Up @@ -74,11 +74,14 @@ class PPMusicImageShadow: UIView {

var imageView : UIImageView!
var blurredImageView : UIImageView!
var blurWork : DispatchWorkItem?

func calculateBlurredImage(){


#if !TARGET_INTERFACE_BUILDER

DispatchQueue.global(qos: DispatchQoS.QoSClass.userInteractive).async {
blurWork?.cancel()
blurWork = DispatchWorkItem(qos: DispatchQoS.userInteractive, flags: DispatchWorkItemFlags.noQoS) {

if let imageToblur = self.image{

Expand All @@ -101,25 +104,16 @@ class PPMusicImageShadow: UIView {
}

guard
let resizedImage = containerImage.resized(withPercentage: self.resizeConstant),
let ciimage = CIImage.init(image:resizedImage),
let resizedImage = containerImage.resized(withPercentage: self.resizeConstant),
let ciimage = CIImage.init(image:resizedImage),
let blurredImage = self.applyBlur(ciimage: ciimage) else {return}

let data1 = UIImageJPEGRepresentation(resizedImage, 1.0)
let dir1 = NSHomeDirectory().appending("/Hellcont.jpg")
try? data1?.write(to: URL.init(fileURLWithPath: dir1))
print("dir : \(dir1)")
let data = UIImageJPEGRepresentation(blurredImage, 1.0)
let dir = NSHomeDirectory().appending("/Hello.jpg")
try? data?.write(to: URL.init(fileURLWithPath: dir))
print("dir : \(dir)")

DispatchQueue.main.async {
self.blurredImageView?.image = blurredImage
}
}
}

blurWork?.perform()

#else
self.blurredImageView?.image = self.image
Expand Down Expand Up @@ -185,9 +179,8 @@ class PPMusicImageShadow: UIView {
override func layoutSubviews() {
super.layoutSubviews()

self.calculateBlurredImage()
imageView?.frame = self.bounds

self.blurredImageView?.frame = self.bounds
self.blurredImageView?.frame.size = getNewImageSize().scaled(by: shadowSizeConstant)

Expand All @@ -202,7 +195,6 @@ class PPMusicImageShadow: UIView {
mask.frame = self.blurredImageView!.bounds
//self.blurredImageView?.layer.mask = mask
self.blurredImageView?.layer.masksToBounds = false

}

override init(frame: CGRect) {
Expand Down Expand Up @@ -232,10 +224,10 @@ class PPMusicImageShadow: UIView {

imageView = UIImageView()
blurredImageView = UIImageView()
self.calculateBlurredImage()
self.addSubview(blurredImageView!)
self.addSubview(imageView!)
self.imageView?.contentMode = self.contentMode
self.blurredImageView?.contentMode = self.contentMode
self.calculateBlurredImage()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="image" value="italy-2080072_640.jpg"/>
<userDefinedRuntimeAttribute type="number" keyPath="blurRadius">
<real key="value" value="3"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="T17-Ox-kpO">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ViewController: UIViewController {

@IBAction func slider1(_ sender: UISlider) {

imageView.blurRadius = CGFloat(sender.value * 8)
imageView.blurRadius = CGFloat(sender.value * 3)
}

@IBAction func slider2(_ sender: UISlider) {
Expand All @@ -29,6 +29,10 @@ class ViewController: UIViewController {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}


}
Expand Down

0 comments on commit ce4cba8

Please sign in to comment.