Cocos2d CCSPrite dimension issues - cocos2d-iphone

self.background = [CCSprite spriteWithFile:#"LetterBrick.png"];
CCLOG(#"background contentSize: (w: %d, h: %d)", self.background.contentSize.width, self.background.contentSize.height);
CCLOG(#"background boundingBox: (x: %d, y: %d, w: %d, h: %d)", self.background.boundingBox.origin.x, self.background.boundingBox.origin.y, self.background.boundingBox.size.width, self.background.boundingBox.size.height);
/**
OUTPUT:
background contentSize: (w: 0, h: 1078984704)
background boundingBox: (x: 0, y: -1069547520, w: 0, h: -1069547520)
*/
So basicly I'm just creating a CCSprite from a file, and printing out the contentsize and the boundingbox. But those values seems a little odd.
Can you help me please? :)

Use floats instead of integer when printing them out

Related

How to add circle annotation in pdfkit swift?

I am using pdfkit and added circle annotation with fixed size and width but i want to draw with dynamic height and width. Here is my code :
Here : start is my CGPoint from where i start to finger
end is second CGPoint where i ended to move finger.
Used start.x and end.y
let circle = PDFAnnotation(bounds: CGRect(x: start.x, y: end.y, width: 100, height: 100), forType: .circle, withProperties: nil)
circle.color = hexStringToUIColor(hex: "#0000FF")
let border = PDFBorder()
border.lineWidth = 3.0
circle.border = border
page?.addAnnotation(circle)
This is second approach to draw circle with dynamic height and width:
Here is code :
let centerX = (start!.x + end!.x)/2
let centerY = (start!.y + end!.y)/2
var distance = (end!.x - centerX) * 2
if distance < 0 {
distance = (start!.x - centerX) * 2
}
let halfDistance = distance/2
self.annotation = PDFAnnotation(bounds: CGRect(x: centerX - halfDistance, y: centerY - halfDistance, width: distance, height: distance), forType: .circle, withProperties: nil)
let page = self.pdfview.currentPage
annotation.color = hexStringToUIColor(hex: "#0000FF")
let border = PDFBorder()
border.lineWidth = 3.0
annotation.border = border
page?.addAnnotation(annotation)
Second approach draws circle with dynamic height and width but not as i want. if i draw circle their are 8 cases :
Finger swiped from left to right - It draw circle on proper position.
Finger swiped from right to left - It draw circle on proper position.
Finger swiped from top left to bottom right - It draw circle half of size
Finger swiped from bottom right to top left - It draw circle half of size
Finger swiped from top to bottom - circle radius value is 2 or 3 width and height
Finger swiped from bottom to top - circle radius value is 2 or 3 width and height
Finger swiped from top right to bottom left - It draw circle half of size
Finger swiped from bottom left to top right - It draw circle half of size
You can use this code to draw circle over a pdfpage
let size = CGSize(width: abs(point.x - startPoint.x), height: abs(point.y - startPoint.y))
var rect = CGRect(origin: startPoint, size: size)
if point.y - startPoint.y < 0 && point.x - startPoint.x < 0
{
rect = CGRect(origin: point, size: size)
}
else if point.y - startPoint.y > 0 && point.x - startPoint.x < 0
{
rect = CGRect(origin: CGPoint(x: point.x, y: startPoint.y), size: size)
}
else if point.y - startPoint.y < 0 && point.x - startPoint.x > 0
{
rect = CGRect(origin: CGPoint(x: startPoint.x, y: point.y), size: size)
}
let page = docView.currentPage
let pageBounds = page!.bounds(for: .cropBox)
let newAnnotation = PDFAnnotation(bounds: pageBounds, forType: .circle,withProperties:nil)
newAnnotation.setRect(rect, forAnnotationKey: .rect)
newAnnotation.color = UIColor.black
page!.addAnnotation(newAnnotation)

extension file making image flip upside down (swift3)

The following extension file generates my image upside down. All i need to do is flip my image by 180 degrees.
case .landscapeLeft:
var transform: CGAffineTransform = CGAffineTransform.identity
transform = transform.translatedBy(x: self.size.width, y: self.size.height)
transform = transform.rotated(by: CGFloat(Double.pi/2))
guard let cgImage = self.cgImage, let colorSpace = cgImage.colorSpace, let context: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return self }
context.concatenate(transform)
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
guard let transformed = context.makeImage() else { return self }
return UIImage(cgImage: transformed)
return imageResult!
I have tried to use 3 * Double.pi / 2 but that makes no image appear on the image view. The only math formula that gets a image on the image view is double.pi / 2 for my code.
Your code rotates the image about the origin of the image which happens to be the top left hand corner. You need to translate the origin to be the centre of the image before applying the rotation and then after translate the origin back to the top left corner. If you replace your transform construction with the following it should work
var transform: CGAffineTransform = CGAffineTransform.identity
transform = transform.translatedBy(x: self.size.width/2, y: self.size.height/2)
transform = transform.rotated(by: angle)
transform = transform.translatedBy(x: -self.size.width/2, y: -self.size.height/2)

How to scale CAShapeLayer to different UIImageView

I have a UIImageView that you can tap on and it draws a circle. I store the location of the circles in an Array of Dictionaries. This allows me to "replay" the drawing of the circles. However, when the UIImageView is a different size from the original, the circles don't scale to the new UIImageView.
How can I get the circles to scale? For demonstration purposes, the top picture is the size of the UIImageView used for input and the second one is the size for replay.
Inputing the circles:
Replay the circles (the circles should be in the blue UIImageView
import Foundation
import UIKit
class DrawPuck {
func drawPuck(circle: CGPoint, circleColour: CGColor, circleSize: CGFloat, imageView: UIImageView) {
let circleBezierPath = UIBezierPath(arcCenter: CGPoint(x: circle.x,y: circle.y), radius: CGFloat(circleSize), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circleBezierPath.cgPath
//change the fill color
shapeLayer.fillColor = circleColour
//you can change the stroke color
shapeLayer.strokeColor = UIColor.white.cgColor
//you can change the line width
shapeLayer.lineWidth = 0.5
imageView.layer.addSublayer(shapeLayer)
}
}
I was able to resolve this with CATransform3DMakeScale As long as I keep the original aspect ratio of the original image it works great.
let width = yellowImageView.frame.width / blueImageView.frame.width
let height = yellowImageView.frame.height / blueImageView.frame.height
shapeLayer.transform = CATransform3DMakeScale(height, width, 1.0)
view.layer.addSublayer(shapeLayer)

AVCapture and zooming of previewLayer in Swift

I have a camera app which allows the user to both take pictures and record video. The iPhone is attached to a medical otoscope using an adapter, so the video that is captured is very small (about the size of a dime). I need to be able to zoom the video to fill the screen, but have not been able to figure out how to do so.
I found this answer here on SO that uses ObjC but have not had success in translating it to Swift. I am very close but am getting stuck. Here is my code for handling a UIPinchGestureRecgoznier:
#IBAction func handlePinchGesture(sender: UIPinchGestureRecognizer) {
var initialVideoZoomFactor: CGFloat = 0.0
if (sender.state == UIGestureRecognizerState.began) {
initialVideoZoomFactor = (captureDevice?.videoZoomFactor)!
} else {
let scale: CGFloat = min(max(1, initialVideoZoomFactor * sender.scale), 4)
CATransaction.begin()
CATransaction.setAnimationDuration(0.01)
previewLayer?.transform = CGAffineTransform(scaleX: scale, y: scale)
CATransaction.commit()
if ((captureDevice?.lockForConfiguration()) != nil) {
captureDevice?.videoZoomFactor = scale
captureDevice?.unlockForConfiguration()
}
}
}
This line...
previewLayer?.transform = CGAffineTransform(scaleX: scale, y: scale)
... gives me the error 'Cannot assign value of type 'CGAffineTransform' to type 'CGTransform3D'. I'm trying to figure this out but my attempts to fix this have been unfruitful.
Figured it out: Changed the problematic line to:
previewLayer?.setAffineTransform(CGAffineTransform(scaleX: scale, y: scale))

How to display text in OpenGL at specific coordinates?

I want to display text on game screen on specific co-ordinate position in OpenGL.
For example a players score "Player Score:" 10 at coordinates (5,5).
How do I do that?
Use a tool called GLFont under ortho view you can output text like
glFontBegin(&font);
glScalef(8.0, 8.0, 8.0);
glTranslatef(30, 30, 0);
glFontTextOut("Test", 5, 5, 0);
glFontEnd();
glFlush();
you can find it here http://students.cs.byu.edu/~bfish/glfontdl.php
i remember there is function under opengl that can put text on screen too.
Check this:
http://www.opengl.org/resources/features/fontsurvey/
EDIT: check this link out too http://mycodelog.com/2010/03/23/printw/
Usage is as simple as calling printf:
printf( "char: %c, decimal: %d, float: %f, string: %s", 'X', 1618, 1.618, "text");
printw(x, y, z, "char: %c, decimal: %d, float: %f, string: %s", 'X', 1618, 1.618, "text");