cocos2d background image dimmensions - iphone

I'm trying to get started with cocos2d by creating an app with a background image. It's not a texture, just a straight image.
I add the background using:
CCSprite* background = [CCSprite spriteWithFile:#"paris.png"];
background.tag = 1;
background.anchorPoint = CGPointMake(0, 0);
[self addChild:background z:0];
The image is 960x640 but when I run it in the iPhone 5 simulator I only see a small part of the image. It's like it's too large for the screen. I was under the impression I would need 960x640. Is this not accurate? What resolution should my image be?
I've tried with and without the anchorPoint being set. Without the anchor I see a smushed image on 1/2 the screen.

Learn about retina display in cocos2d: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:how_to_develop_retinadisplay_games_in_cocos2d
Make an image with 480x320 with name paris.png...
Rename your current image as paris-hd.png...
Enable Retina Display in app delegate.. And you are done.. Hope this helps.. :)

Related

iOS - SpriteKit and image dimessions

I just started using SpriteKit and I am trying to put a background on my scene. I want to support iPhone 5,5S,6,6+. So I created background with dimensions(using image casset: #1x at 320x480 , #2x at 750x1334 and #3x 1242x2208.
Then I am creating a Sprite:
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:#"Backgroun.png"];
background.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:background];
but the background is not covering the whole screen. What I am doing wrong here?
I found a workaround:
background.size = self.frame.size;
but this is not right! I want to know what dimessions the background has to be for each device.
Thanks in advance.

CCAnimation shownig as black rectangle on iPod 4G

I create an animation like this:
//xxx animation cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"mainmenu_xxx.plist"];
//xxx blink
CCAnimation *animXxx1Anim = [CCAnimation animationWithSpriteSequence:#"char_xxx_loops%##2x.png" frameSequence:#"02,03,04" delay:1/24.0];
[animXxx1Anim setRestoreOriginalFrame:NO];
[[CCAnimationCache sharedAnimationCache] addAnimation:animXxx1Anim name:#"xxxBlink"];
and then run it like this:
//blink animation
[animatedXxxSprite runAction:[CCAnimate actionWithAnimation:[[CCAnimationCache sharedAnimationCache] animationByName:#"xxxBlink"]]];
It shows as it should on simulator, iPhone 3GS, iPhone 4S, but it shows as a black rectangle on iPod 4G. Any idea what could be causing it?
The problem was with the texture size. It is recomended that the texture size is lower than 1024x1024 pixels.
Also, sometimes, textures are shown with a very small (1-2 px) position error if it is not in POT format.

How to migrate apps for iPhone 5 screen resolution?

I have an application in iphone in which i am using most of the static images in the background as an image in the background image it self.Means a single background image including all the staic things.now i want to migrate to iphone 5 also.Now i am using an image of 640*960 as the background image.if i change that to 640*1136 then when it used in 4serieses of iphone the image will shrink i think.Let it i am fixing the frame size by checking the screeen.Can anybody help me how i can resolve this issue?
To know, what kind of image You should use (for iPad 768x1024, for iPhone 320x480, etc..)
You can detect device, and then set corresponding image.
You can distinguish between iPad/Retina/iPhone using this SO article:
Detect retina screen/iPhone 4 in iPhone SDK
You can detect iPhone 5 using example in this SO article:
How to detect iPhone 5 (widescreen devices)?
If you have only portrait supported, You can check using this simple method:
if([[UIScreen mainScreen] bounds].size.height == 480)
{
//iPhone, iPhone retina
}
else if([[UIScreen mainScreen] bounds].size.height == 1024)
{
//iPad, iPad retina
}
else //568 height
{
//iPhone 5
}
And of course - as Valeriy Van mentioned, You need Default-568h#2x.png image added to your bundle.
There is no connection between image resolution and size of UIImageView. Image will be rescaled or positioned inside view bounds depending on contentMode property.
What you need first - add Default-568h#2x.png image of 640x1136 as Launch Image for Retina 4 inch. If springs and struts of your views are set right, everything will work as granted.

Getting Blank Image while trying to capture MPMoviePlayerViewController view using UIGraphicsGetImageFromCurrentImageContext()

I have an requirement to capture iPhone screen when my app is in foreground . I have used UIGraphicsGetImageFromCurrentImageContaxt() for this it works in most of synerio but fails when video is playing by using MPMoviePlayerViewController or AVPlayer and gives back black image with player control.
Probabely My guess is MPMoviePlayerViewController rendering frames using OpenGl and method UIGraphicsGetImageFromCurrentImageContaxt() is not able to capture the image ??
I am missing something or is there any alternative soln available to capture iPhone Screen while app is in foreground ??
There is not any easy solution(i.e. might be I don't know) for this but you need to figure out. Here I have described possible solutions. When you will try simply rendering the view on context then it will give blank screen in place of player and other things will be as it is.
Possible solutions that I know
Private API
You can use UIGetScreenImage() function to capture whole screen of device including player and its control. This is the way you can get the image of player with view easily.
Note: Some buddies are saying use of this function may cause rejection of application(I have never used for app store's app so I don't have much experience about it :)).
Second way.
If you want to get image of player with the other things that resides in main view or iPhone screen then basic idea is to capture two images(i.e. One of movie player and another of whole screen like you are doing using UIGraphicsGetImageFromCurrentImageContext) and combine them as a one image using some proper calculations.In this way you need to do some calculations so accuracy is depend upon your calculations.
Here I am preferring to use AVPlayerinstead of MPMoviePlayer not sure I am true or not but as I have feel that AVPlayer providing exact frame at certain time(Good accuracy. Exact image that is showing on the screen.)
Please set the gravity mode of AVPlayer to AVLayerVideoGravityResizeAspect. This mode will preserver aspect ratio and fit within layers bounds(i.e. This option is default). You can set this way.
playerLayer.videoGravity = setVideoFillMode:AVLayerVideoGravityResizeAspect;
where playerLayer is the object of AVPlayerLayer.
Now get the image of AVPlayerLayer
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]
initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform=YES;
/*AVAssetImageGenerator will scale images such that they fit within the defined bounding box. Images will never be scaled up. So provide this should be the size of AVPlayer. */
imageGenerator.maximumSize=CGSizeMake(400, 400);
CGImageRef imgRef = [imageGenerator copyCGImageAtTime:storedCMTime actualTime:NULL error:NULL];
[imageGenerator release];
Please note that this generated image might be not exact size for what you are looking for or as you have exactly provided in maximum size because as I mentioned in above comment here image will be never scaled up. if image is not the exact size for what you are looking then use some image utility function to scale the image as per the size of your player but don't spoil Aspect ration because we have set the AVPLayer's mode to AVLayerVideoGravityResizeAspect.store this image temporary.
Now Capture the image of View using UIGraphicsGetImageFromCurrentImageContext(i.e. like you are doing currently). Draw image of AVplayer that we have captured and stored over exactly the black area that is showing on your view(i.e. You need to do some trial and error to get the exactly same).
Here I have tried to cover all the main points that may cause panic or not immediately obvious.
Update:
Screen capturing solutions from APPLE: http://developer.apple.com/library/ios/#qa/qa1703/_index.html#//apple_ref/doc/uid/DTS40010193
If you would like to go with MPMoviewPlayerController then it's okay. Get the thumbnail using code shows by #Kuldeep. And combine the Both view's image and Thumbnail using Image Masking exactly explain here : Link
I would rather suggest to use below snippet, may it would be more easy to use. You just need to pass time factor at which you need to capture the video.
float timeFactor = 60.0;
CGRect rect = CGRectMake(self.view.center.x, self.view.center.y, 100.0, 100.0)
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:timeFactor timeOption:MPMovieTimeOptionNearestKeyFrame];
UIImageView *imagV=[[UIImageView alloc]initWithImage:thumbnail];
[imagV setFrame:rect];
Well, it seems I've found a solution to capture frames in a AVPlayer:
- (UIImage*) captureFrame {
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:[self.player.currentItem asset]];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5) {
[imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];
[imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
}
CGImageRef ref = [imageGenerator copyCGImageAtTime:self.player.currentItem.currentTime actualTime:nil error:nil];
UIImage *img = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
return img;
}

PNG Transparency in cocos2d iphone issue

I've just begun cocos2d development on the iphone. An issue I'm having is that when I add a CCSprite to my scene, instead of it being transparent around the edges of the .png file, its showing up as white.
So for example, I define the CCSprite in my header file -
CCSprite *foo;
Then when I initialise my scene -
foo = [CCSprite spriteWithFile:#"foo.png"];
[self addChild:player z:0 tag:1];
Is there anything wrong with this code?
Thanks for any help.
P.S. I have double checked my .png file in Photoshop and its correctly showing up as transparent where it should be.
Check the bottom portion of this page:
http://www.cocos2d-iphone.org/wiki/doku.php/faq#my_png_doesn_t_look_like_in_photoshop