I try to get frame from navigationBar and get zero width and height of size. Code:
CGRect f = self.navigationController.navigationBar.frame;
In debug mode I get zero from f.height.
What is wrong?
use this
NSLog(#"frame:%#",NSStringFromCGRect(f));
This will show frame.
Most probably you are logging int as float or float as int. If you do
int ih = f.size.height;
or
float fh = f.size.height;
you will not see any warning as float will be converted to float, but trying to log
NSLog("%d", fh);
or
NSLog("%f", ih);
should give you a warning, just read it.
in your code : f.height should be f.size.height
By doing so : this code
CGRect f = self.navigationController.navigationBar.frame;
NSLog(#"f.size = (%.2f, %.2f)", f.size.width, f.size.height);
is giving me this output :
2012-01-14 15:54:32.909 naveCon[4071:207] f.size = (320.00, 44.00)
Related
Im trying to cast the results of a calculation (ShotPercentage) to a Float and present the results in the App as a 89 percent for example. But I am struggling with the type casting any help would be greatly appreciated. Here is my code:
// calculate shot percentage
shotPercentage = makeCounter / totalCounter
shotPercentageLabel.text = "\(shotPercentage)"
You can apply a conversion to your Floatresult like this.
shotPercentage = Int(makeCounter / totalCounter)
var shotPercentageInt: Int
shotPercentageInt = 3/5
println("\(shotPercentageInt*100)%") // 0% because 3/5 = 0.6 -> Int = 0
//
var shotPercentageFloat: Float
shotPercentageFloat = 3/5
println("\(shotPercentageFloat*100)%") // 60.0% because 3/5 = 0.6 -> Float = 60%
// Convert Float to Int
var shotPercentageFloatToInt: Int
shotPercentageFloatToInt = Int(shotPercentageFloat)
println("\(shotPercentageFloat)") // 0.6
// It's amazing
You might want to use NSNumberFormatter. It does well going to strings and coming from strings. Regardless, it would look like:
let makeCounter = 15
let totalCounter = 20
let shotPercentage:Float = makeCounter / totalCounter
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
if let percentString = formatter.stringFromNumber(shotPercentage) {
shotPercentageLabel.text = percentString
}
In this case the label would read 75%, as the formatter will include the percent sign for you.
I am trying to determine a label's position so as to place other labels below.
When I try to output the data on a specific label I get 0.000 as a output on log.
Any idea why this is happening?
...
postTextLabel.text = postText;
postTextLabel.frame = CGRectMake(postTextLabel.frame.origin.x,
postThumbView.frame.origin.y + postThumbView.frame.size.height,
postTextLabel.frame.size.width,
postTextLabel.frame.size.height);
[postTextLabel resizeToFit];
postAuthorNameLabel.text = postAuthorName;
postTimestampLabel.text = postTimestamp;
NSLog(#"%f", postTimestampLabel.frame.origin.y);
I am developing an application in which I want to add echo effect in recorded audio files using objective-c.
I am using DIRAC to add other effect e.g. man to women, slow, fast.
now I have to make Robot voice of recorded voice. for robot voice I need to add echo effect
Please help me to do this
Echo is pretty simple. You need a delay line, and little multiplication. Assuming one channel and audio already represented in floating point, a delay line would look something like this (in C-like pseudo-code):
int LENGTH = samplerate * seconds; //seconds is the desired length of the delay in seconds
float buffer[ LENGTH ];
int readIndex = 0, writeIndex = LENGTH - 1;
float delayLine.readNext( float x ) {
float ret = buffer[readIndex];
++readIndex;
if( readIndex >= LENGTH )
readIndex = 0;
return ret;
}
void delayLine.writeNext( float x ) {
buffer[ writeIndex ] = x;
++writeIndex;
if( writeIndex >= LENGTH )
writeIndex = 0;
}
Don't forget to initialize the buffer to all zeros.
So that's your delay line. Basic usage would be this:
float singleDelay( float x ) {
delayLine.writeNext(x);
return delayLine.readNext( x );
}
But you won't hear much difference: it'll just come out later. If you want to hear a single echo, you'll need something like this:
float singleEcho( float x, float g ) {
delayLine.writeNext(x);
return x + g * delayLine.readNext( x );
}
where g is some constant, usually between zero and one.
Now say you want a stream of echos: "HELLO... Hello... hello... h..." like that. You just need to do a bit more work:
float echo( float x, float g ) {
float ret = x + g * delayLine.readNext( x );
delayLine.writeNext( ret );
return ret;
}
Notice how the output of the whole thing is getting fed back into the delay line this time, rather than the input. In this case, it's very important that |g| < 1.
You may run into issues of denormals here. I can't recall if that's an issue on iOS, but I don't think so.
Okay so I'm trying to get my values rounded up to the nearest whole number with an IBAction.
So 1.88 -> 2.00 ,
11.40 -> 12.00 ,
111.01 -> 112.00, etc.
-
-(IBAction)roundUp:(id)sender
{
float floatRoundValue=lblTotalRound.text floatValue];
ceilf(floatRoundValue);
NSString *stringRoundValue = [NSString stringWithFormat:#"%1.0f", floatRoundValue];
lblTotalRound.text=stringRoundValue;
}
That's what I got. But it's still rounding down below .5 and to the nearest integer
(Ex. 1.19 -> 1 , i need 1.19 -> 2.00).
I've tried %1.2f but the value doesnt change at all.
What am I doing wrong?
ceilf doesn't modify the value you pass in. It returns a modified value.
floatRoundValue = ceilf(floatRoundValue);
ceilf is a function that returns a value (https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man3/ceil.3.html) , you simply need to change that line to the following.
floatRoundValue = ceilf(floatRoundValue);
So the complete code would in your case it would be something like this.
-(IBAction)roundUp:(id)sender
{
float floatRoundValue=lblTotalRound.text floatValue];
floatRoundValue = ceilf(floatRoundValue);
NSString *stringRoundValue = [NSString stringWithFormat:#"%1.0f", floatRoundValue];
lblTotalRound.text=stringRoundValue;
}
If you need to have a $ sign before :
-(IBAction)roundUp:(id)sender
{
float floatRoundValue=(lblTotalRound.text floatValue];
floatRoundValue = ceilf(floatRoundValue);
NSString *stringRoundValue = [NSString stringWithFormat:#"$%1.0f", floatRoundValue];
lblTotalRound.text=stringRoundValue;
}
NSString Class Reference
I just want to:
1) Copy the pixel data.
2) Iterate and Modify each pixel (just show me how to NSLog the ARGB values as 255)
3) Create a UIImage from the new pixel data
I can figure out the the gory details if someone can just tell me how to NSLog the RGBA values of a pixel as 255. How do I modify the following code to do this? Be Specific Please!
-(UIImage*)modifyPixels:(UIImage*)originalImage
{
NSData* pixelData = (NSData*)CGDataProviderCopyData(CGImageGetDataProvider(originalImage.CGImage));
uint myLength = [pixelData length];
for(int i = 0; i < myLength; i += 4) {
//CHANGE PIXELS HERE
/*
Sidenote: Just show me how to NSLog them
*/
//Example:
//NSLog(#"Alpha 255-Value is: %u", data[i]);
//NSLog(#"Red 255-Value is: %u", data[i+1]);
//NSLog(#"Green 255-Value is: %u", data[i+2]);
//NSLog(#"Blue 255-Value is: %u", data[i+3]);
}
//CREATE NEW UIIMAGE (newImage) HERE
return newImage;
}
Did this direction work for you? I'd get pixel data like this:
UInt32 *pixels = CGBitmapContextGetData( ctx );
#define getRed(p) ((p) & 0x000000FF)
#define getGreen(p) ((p) & 0x0000FF00) >> 8
#define getBlue(p) ((p) & 0x00FF0000) >> 16
// display RGB values from the 11th pixel
NSLog(#"Red: %d, Green: %d, Blue: %d", getRed(pixels[10]), getGreen(pixels[10]), getBlue(pixels[10]));
If you'd like to actually see the image, you can use Florent Pillet's NSLogger:
https://github.com/fpillet/NSLogger
The idea is you start the NSLogger client on your desktop, and then in your app you put this up towards the top:
#import "LoggerClient.h"
And in your modifyPixels method you can do something like this:
LogImageData(#"RexOnRoids", // Any identifier to go along with the log
0, // Log level
newImage.size.width, // Image width
newImage.size.height, // Image height
UIImagePNGRepresentation(newImage)); // Image as PNG
Start the client on your desktop, and then run the app on your iphone, and you'll see real images appear in the client. VERY handy for debugging image problems such as flipping, rotating, colors, alpha, etc.