What do the original frame and frame methods do - framerjs

Having a hard time understanding this method for making imported layers available on the root:
for layerGroupName of PSD
window[layerGroupName] = PSD[layerGroupName]
for layerGroupName of PSD
PSD[layerGroupName].originalFrame = window[layerGroupName].frame
What do the original frame and frame methods do, or where are they documented?

this confused me too at first, but I found cues to answer this question in this article by Cemre Güngör.
for layerGroupName of PSD
window[layerGroupName] = PSD[layerGroupName]
This one copies all the layers from the original object with imported layers to the window object. This way all the layers become available using layerName instead of PSD['layerName'].
for layerGroupName of PSD
PSD[layerGroupName].originalFrame = window[layerGroupName].frame
frame and originalFrame here are actually keys in an object, not methods. These two lines copy the initial frame (including position of the layer etc.) of each layer under the originalFrame key, so that when you change the position of layers, you can easily revert them to their original position.

Related

Changing a view's values and its layer's values

I'm animating an ImageView using CABasicAnimation.
I move its layer to left, right, up and down and sometimes I'd scale it bigger then reset it to its original size etc.
I'm doing all this to its layer so I thought I might have to move & scale the real thing along with its layer as well but when I tested it with tap gesture to see if it really was just staying where it started, it wasn't. Therefore I no longer need to change its view's frames as far as I'm concerned.
Is changing a view's layer's values also change its view's values?
A UIView is no more than a fancy wrapper for a CALayer – bringing UIResponder events & animation conveniences among many other things.
Many UIView properties are actually just forwarded versions of the underlying CALayer properties, defined purely for convenience.
A view's frame & bounds properties should always reflect the layer equivalents.
transform is slightly more complex, as for the view it's of type CGAffineTransform – whereas on the layer it's CATransform3D. If the layer's transform can be represented as a CGAffineTransform, then you'll be able to access it from the view after setting it on the layer. If it can't be represented, then its value is undefined.
Therefore yes, you are right in saying you don't need to update the frame or transform on the UIView when changing it on its CALayer. Although note that these properties won't reflect the 'in-flight' values of the animation – you'll need to access the layer's presentationLayer for that.
Also note that as #par & #jrturton mention, if a layer's transform is not the identity transform, then the frame is undefined and you therefore shouldn't use it.

PaintCode, How to add a frame around a portion of a vector and have it dynamically resize?

I've imported a vector layer from a psd into paint code v1, I'm trying to create a background image and make it universal.
I can't seem to add a frame around the vector, to complicate matters, I only need a portion, the center, of the layer. (The design is based around a circle, it has lines drawn towards the center of the circle.)
I can’t seem to add a frame to dynamically resize the part I need.
I found this http://www.raywenderlich.com/36341/paintcode-tutorial-dynamic-buttons the part about frame ans groups doesn't help me....
When I add a click frame and drag it around the area I need, it's at the same level as the vector layer. I've also tried adding a group around both, but that doesn't seem to obey the frame size either.
I’ve looked through the tutorials and googled adding a frame, but I can’t seem to achieve what I need.
EDIT
A frame is supposed to be at the same level as the vectors you're working with.
All you do then is set the resize rules of your vectors. There is a little rectangle in the frame's parameters interface with straight arrows and springs that you can modify to fit your wishes.
I think I also remember a checkbox setting to resize only what's inside the frame.
Now I haven't used PaintCode for a while, but if this doesn't help you, there probably is a problem with your vector layer.
I don't know if this information helps.
But if you resizing doesn't work as you expected. Look carefully at the transformation box (the one with the springs attached). When you have put a frame around your object. The middle dot in this box should be blue instead of green. if t's green, you may have a problem with the originating point of your objects and then the resizing may not work as you expected.

Set blendmodes on UIImageViews like Photoshop

I've been trying to apply blend modes to my UIImageViews to replicate a PSD mock up file (sorry can't provide). The PSD file has 3 layers, a base color with 60% normal blend, an image layer with 55% multiply blend and a gradient layer with 35% overlay.
I've been trying several tutorials over the internet but still could not get the colors/image to be exactly the same.
One thing I noticed is that the color of my iPhone is different from my Mac's screen.
I found the documentation for Quartz 2D which I think is the right way to go, but I could not get any sample/tutorial about Using Blend Modes with Images.
https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-CJBIJEFG
Can anyone provide a good tutorial that does the same as the one in the documentation so I could atleast try to mix things up should nobody provide me a straight forward answer to my question.
This question was asked ages ago, but if someone is looking for the same anwser, you can user compositingFilter on the backing layer of your view to get what you want:
overlayView.layer.compositingFilter = "multiplyBlendMode"
Suggested by #SeanA, here: https://stackoverflow.com/a/46676507/1147286
Filters
Complete list of filters is here
Or you can print out the compositing filter types with
print("Filters:\n",CIFilter.filterNames(inCategory: kCICategoryCompositeOperation))
Now built-in to iOS. Historic answer:
You don't want to use UIImageView for this since it doesn't really support blend modes.
Instead, the way to go would be to create a UIView subclass (which will act just like UIImageView). So make a UIView subclass called something like BlendedImageView. It should have an image property, and then in the drawRect: method you can do this:
- (void)drawRect:(CGRect)rect
{
//
// Here you need to calculate a rect based on self.contentMode to replicate UIImageView-behaviour.
// For example, for UIViewContentModeCenter, you want a rect that goes outside 'rect' and centers with it.
//
CGRect actualRect = // ...
[self.image drawInRect:actualRect blendMode:kCGBlendModeOverlay alpha:0.5];
}
Replace alpha and blend mode to fit your preference. Good practice would be to let your BlendedImageView save a blendMode property.
It may be that in your case, you need to do a bit more advanced view that draws all 3 of your images, using the same code as above.

How to find the difference between one frame to another for a specified part of an image only

I'm using OpenCV for my project on video feature tracking. I need to create a mask which is the difference between one frame to the next one. I know to do this, we can use the cv.absdiff function. The problem is I want the mask to contain only the difference for a specified small part of the frame, i.e. not the entire image. I'm not sure how to go about doing this.
// define regions of interest in images
Rect roiRect1(x1, y1, roiWidth, roiHeight);
Rect roiRect2(x2, y2, roiWidth, roiHeight);
// create images of regions of interest. no copy is performed, this is just new pointers to existing data
Mat roiImage1(frame1, roiRect1);
Mat roiImage2(frame2, roiRect2);
// do whatever you want with those images
......

Duplicate CALayer

I want to create a PDF from a UIView.
Now I want to copy the layer and resize it to a DIN A4 page size.
CGRect A4PageRect = CGRectMake(0, 0, 595, 842);
CALayer *myLayer = [pdfView layer];
myLayer.bounds = pageRect;
But this cody resizes the visible layer on my screen.
How can I copy the layers contents to resize it to fit an A4 page?
Thanks for help, Julian
FWIW, you can make a shallow copy of a CALayer via:
(Swift 3)
let tmp = NSKeyedArchiver.archivedData(withRootObject: myLayer)
let copiedCA = NSKeyedUnarchiver.unarchiveObject(with: tmp) as! CALayer
(or whatever subclass you are using. e.g. CAShapeLayer)
Thanks to: https://stackoverflow.com/a/35345819/1452758
There is no way to duplicate a CALayer.
(It would be difficult for CoreAnimation to implement that in a sensible way. There might be a whole tree of sublayers, and they all might have delegates that influence their behavior, which wouldn't expect to suddenly get requests from the copies of the layers.)
I can only guess at a better solution, because I don't understand your exact situation. Do you have a PDF that you are trying to resize, or do you just want take an arbitrary existing layer and make a PDF document out of it?
If the latter:
Use UIGraphicsBeginPDFContextToData or UIGraphicsBeginPDFContextToFile to create a PDF drawing context
Call UIGraphicsBeginPDFPage or UIGraphicsBeginPDFPageWithInfo to create a page
Call UIGraphicsGetCurrentContext to get the PDF drawing context
Scale using CGContextScaleCTM so your layer will fit in the PDF page
Call -[CALayer renderInContext:] to draw the layer into the PDF context
Call UIGraphicsEndPDFContext to finish the PDF
Note that this may look terrible. Layers are bitmap-based, so you'll get a bitmap in your PDF. Also, -[CALayer renderInContext:] doesn't render exactly the same as it does on-screen -- see the note in the documentation.
If this is a problem, you'll need to add a separate drawing path that bypasses CALayer. In step 5, you would do your own drawing using CoreGraphics.
In your case it's indeed better to go with the proposed solution but for the record, yes you can duplicate - in a way - a CALayer : using CAReplicatorLayer.
It's a very powerful API, not necessary build for duplicating one layer but it works for that too. It works even better for making astonishing visual effects by duplicating series of layers.
For more information you can have a look at the apple official documentation, here an extract:
A layer that creates a specified number of copies of its sublayers (the source layer), each copy potentially having geometric, temporal, and color transformations applied to it.
And here a great tutorial by John Sundell, another extract:
CAReplicatorLayer specializes in drawing multiple copies of an original layer (hence it being a "replicator"), in an efficient - hardware accelerated - manner. It's super useful when drawing things like tiled backgrounds, patterns or other things that should be repeated multiple times. I even use it to implement the texture tiling feature of my upcoming open source Swift game engine.
And finally here a great example of what can be done with this api.

Resources