This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I created CCSprite and add it to the main view.
Then I want to see the sprite image, so I saved it to PNG
But lots of them doesn't saved correctly.
Only show white background.
I can't know the reason, and also found some articles via Google, but they didn't help me at all.
How can I solve this?
CCSprite *sprite = [[CCSprite spriteWithFile:[NSString stringWithUTF8String:name] rect:CGRectMake(startx, starty, w, h)] retain];
float drawX = x, drawY = y;
CGSize size = [sprite contentSize];
int nWidth = size.width;
int nHeight = size.height;
nWidth *= scale;
nHeight *= scale;
drawX = drawX + nWidth/2;
drawY = drawY - nHeight/2;
ConvertCoordf(&drawX, &drawY);
drawY -= nHeight;
[sprite setScale:scale];
[sprite setPosition:ccp(drawX, drawY)];
[_mainLayer addChild:sprite];
CCRenderTexture* render = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];
[render begin];
[sprite visit];
[render end];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"new%d.png",i]];
i++;
[render saveBuffer:pngPath format:kCCImageFormatPNG];
[sprite release];
Related
In Xcode i am trying to make a graph into which some points (X,Y) are given by the user.The user can choose how many points he or she wants to plot on the graph. I have stored the X axis points in an NSMutableArray and the Y axis points in an NSMutableArray but now i need to sort these into ascending order so i can fix a bug with the graph when having two points with the same x or y axis plot. The NSMutableArrays contains primitive int's. An example of what i need to do is if i am given 4 coordinates for one of the axis such as 4,8,5,3 i need the array to be sorted into 3,4,5,8
The code below doesn't include the graph generation because it is quite long
Thanks for the help! -
int userInputCount;
printf("How many coordinates would you like to plot? ");
scanf("%i", &userInputCount);
printf("\n");
NSMutableArray * xCoordArray = [[NSMutableArray alloc] init];
NSMutableArray * yCoordArray = [[NSMutableArray alloc] init];
for (int i = 1; i <= userInputCount; i++){
int xCoord;
int yCoord;
printf("(%i) Enter coordinates [X,Y]: ", i);
scanf("%i,%i", &xCoord, &yCoord);
NSNumber *xCoordinate = [[NSNumber alloc] initWithInt:xCoord];
NSNumber *yCoordinate = [[NSNumber alloc] initWithInt:yCoord];
[xCoordArray addObject:xCoordinate];
[yCoordArray addObject:yCoordinate];
}
After working on it for a while I figured it out if anybody is trying the same thing
for (int j = 0; j < 2; j++){
for (int i = 0; i < [yCoordArray count] - 1; i++){
int yExchangeIntOne = [[yCoordArray objectAtIndex:i] intValue];
int yExchangeIntTwo = [[yCoordArray objectAtIndex:i +1] intValue];
if (yExchangeIntOne > yExchangeIntTwo){
[yCoordArray exchangeObjectAtIndex:i+1 withObjectAtIndex:i];
i = 0;
}
int xExchangeIntOne = [[xCoordArray objectAtIndex:i] intValue];
int xExchangeIntTwo = [[xCoordArray objectAtIndex:i +1] intValue];
if (xExchangeIntOne > xExchangeIntTwo){
[xCoordArray exchangeObjectAtIndex:i+1 withObjectAtIndex:i];
i = 0;
}
}
}
This goes right after the
[xCoordArray addObject:xCoordinate]; and the
[yCoordArray addObject:yCoordinate]; in the loop
so I have a [NSMutableArray *paths] that contains UIBezierPaths that correspond to each separate path drawn on the screen. I also have another NSMutable array colors that contains the corresponding color for the path. The problem that I am having is that although the two arrays are mutating and I can see the count of both going up, the following drawRect method only seems to draw the last path in the array.
I have scoured through a lot of questions and based on what I have read so far, this very problem seems to get us newbies and mostly because of type mismatch. I even tried to Typecast the return and it was futile.
Here is my code from the drawRect
int cnt = paths.count;
if (cnt != 0)
{
for(int i = 0; i < cnt; i++)
{
NSLog(#"Drawing path:%i", i);
UIColor *color;
color = [colors objectAtIndex:i];
[color setStroke];
UIBezierPath *path = [paths objectAtIndex:i];
path.lineWidth = 2;
[path stroke];
}
}
every time I call [self setNeedsDisplay] it wipes out my entire screen.
Am I correct in assuming that I am not handling my
UIBezierPath *path = [paths objectAtIndex:i];
well?
Thank you very much and in advance.
UPDATE:
I was rummaging through some more codes and now it almost looks like there is another possibility that ARC is releasing the contents of my NSMutable array. I tried the do the following and got a compiler error.
[paths retain];
Besure you add your path to your path array afer each draw
- (void)drawRect:(CGRect)rect {
[brushPattern setStroke];
UIBezierPath *drawPath = [UIBezierPath bezierPath];
drawPath.lineCapStyle = kCGLineCapRound;
drawPath.miterLimit = 0;
drawPath.lineWidth = thisLineWidth;
for (UIBezierPath *path in pathArray)
[drawPath appendPath:path];
UIBezierPath *path = [self pathForCurrentLine];
if (path)
[drawPath appendPath:path];
[drawPath stroke];
}
- (UIBezierPath*)pathForCurrentLine {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
return path;
}
I made my project to show simple texture by using CCSprite.
But I found that the texture contains some tiny noise , black pixel bug.
I divided the png file into small piece by using code. And add it to the mainview.
Not all of them have noise, But some of them have it.
I can't know the reason.
Please send your opinion.
CCSprite *sprite = [[CCSprite spriteWithFile:[NSString stringWithUTF8String:name] rect:CGRectMake(startx, starty, w, h)] retain];
float drawX = x, drawY = y;
CGSize size = [sprite contentSize];
int nWidth = size.width;
int nHeight = size.height;
nWidth *= scale;
nHeight *= scale;
drawX = drawX + nWidth/2;
drawY = drawY - nHeight/2;
ConvertCoordf(&drawX, &drawY);
drawY -= nHeight;
[sprite setScale:scale];
[sprite setPosition:ccp(drawX, drawY)];
[_mainLayer addChild:sprite];
[sprite release];
For every image, it has some noise pixel around it (Only 1 pixel). For example, if you want to include an image with 30px*30px, you'd better make it into a 31px*31px image and put your image in the middle of it.In other words, surround your image with 1px.
I'm making a game where a grid that is x by y square views big. What I'd like to do is be able to create the grid programmatically.
I had something along these lines in mind.
int across = x
int down = y
CGRect dimentions = foo, bar //etc
int distanceBetween = z
///some crazy loop to make it
If possible I'd also like to keep a reference to each, ideally by assigning tags so I can manipulate them later..
Cheers
S
I think this should do the trick
int across = x
int down = y
CGSize dimensions; // make a cgsize here with the correct values
int distanceBetween = z;
NSMutableArray *views = [[NSMutableArray alloc] init];
for (int x = 0; x < across; x++) {
NSMutableArray *downViews = [[NSMutableArray alloc] init];
for (int y = 0; y < down; y++) {
UIView *view; //create view using the dimensions and padding
//any additional setup to the views
[downViews addObject:view];
}
[views addObject:downViews];
[downViews release];
}
Basically it's just a matter of setting the grid sizes and then calculating the x and y positions from there.
It should calculate how many columns across based on the frame size. So that if the frame widens it will show more columns. Not sure if you need it to be resolution independent or not but I've put that in there.
If you need a set number of columns in the grid then just change gridColumns to be a fixed integer.
float gridItemWidth = 225.00;
float gridItemHeight = 225.00;
-(int)gridColumns {
NSRect frame = [self frame];
return (int)frame.size.width / (gridItemWidth);
}
-(int)getxOffset:(int)i {
int xPadding = 10;
return ((i % [self gridColumns]) * (gridItemWidth + xPadding));
}
-(int)getyOffset:(int)i {
int yPadding = 20;
return (floor(i / [self gridColumns]) * (gridItemHeight + yPadding));
}
Then basically in your loop you call getxOffset and getyOffset for the index of your loop to get the position of the grid.
I have manually set the x and y paddings in there to be 10 and 20, but you can set them to whatever you need.
This question already has answers here:
How do I make UILabel display outlined text?
(15 answers)
Closed 8 years ago.
I was wondering how can I create text stroke for UILabel ? is there any possible way ?
thank you ,
#import <Foundation/Foundation.h>
#interface CustomLabel : UILabel {
}
#end
#import "CustomLabel.h"
#implementation CustomLabel
- (void)drawTextInRect:(CGRect)rect {
CGSize shadowOffset = self.shadowOffset;
UIColor *textColor = self.textColor;
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, 22);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = [UIColor whiteColor];
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
self.shadowOffset = shadowOffset;
//works fine with no warning
}
now the question is how can i use this subclass with a IBOutlet label on different viewcontrollers . is it right :
label = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, 190, 190)];
It may be helpful to some to add that depending on font and characters, adding:
CGContextSetLineJoin(c,kCGLineJoinRound);
can prevent artifacts from too large a stroke applied to too sharp a character.
There is one issue with this implementation. Drawing a text with stroke has a slightly different character glyph width than drawing a text without stroke, which can produce "uncentered" results. You can fix that by adding an invisible stroke around the fill text.
You should replace:
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
with:
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
self.textColor = textColor;
[[UIColor clearColor] setStroke]; // invisible stroke
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
I'm not 100% sure, if that's the real deal, because I don't know if self.textColor = textColor; has the same effect as [textColor setFill], but you get the idea.
Disclosure: I'm the developer of THLabel.
I've released a UILabel subclass a while ago, which allows an outline in text and other effects. You can find it here: https://github.com/tobihagemann/THLabel