UIImage Frame Null in NSMutableArray - iphone

I am trying to display star rating images, and I have three star images: full star, half star and an unselected grey-ed out star.
I have an array which holds the stars: so for rating 4.5, it would hold 4 selected and 1 half. I am adding the same star objects into the array so that I do not have to create multiple instances of the stars. I have just three instances and in my calculations I am just using addObject on those three different images as follows:
for (int i = 0; i < ratingCount; i++) {
if (rating >= 1)
[self.imageViews addObject:self.selected];
else
[self.imageViews addObject:self.halfSelected];
rating--;
}
I am having an issue drawing these images. In a subsequent loop, I am trying to draw them out as follows:
for (int i = 0; i < self.imageViews.count; ++i) {
UIImageView *imageView = [self.imageViews objectAtIndex:i];
imageView.frame = CGRectMake(i * (5 + imageWidth), 0, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
}
This crashes because imageView.frame is coming out as null. When I debugged it, it prints out null <0x00000000>. Why is the frame coming out as null? The images are not printing as null and I know that they are added to the array properly.
When removing imageView.frame, I also get an [UIImage superview]: unrecognized selector sent to instance 0x8644590
The images are instantiated using imageNamed in the init method. Would that cause an issue? Do they get allocated early? That should print null when trying to see the image in the debugger using po so I don't think that is the issue.

You're confusing a UIImage with a UIImageView. Your array contains images, which do not have frames, since they are not views.
You need a separate set of image views, which have the images assigned to them.

Related

multiple uiimageviews from for loop

I am writing a game in which a Uiimageview needs to be 'spawned,' if you will. every time the character hits the end box. Everything else is working except that. Could someone check my code and see what I'm doing wrong? Thanks!
for (int i = 1; i <= level; i++) {
int xPos = rand() % 316;
int yPos = rand() % 450;
UIImageView *mine = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 10, 10)];
}
You are not adding the views that you create in a loop as subviews to your main view. That's why they do not become visible.
As a side note, I think you would be better off using CALayer objects instead of UIImageView: they are lighter-weight, and require about the same amount of work.

Averaging multiple UIImages

I have been searching for this answer for a while. But I haven't been able to find it.
I would like to average the pixels of 30 UIimages. To do so, I would like to do it using Quartz2D instead of going over all the pixels of all the images. It ocurred to me that, in order to paint 30 images together I should just adjust the alpha channel of each of them to 1/30. Then, after painting one in top of the other I would get the desired effect.
the desired formula should be: Dest Px = (img[0].px+....img[29].px)/30
I have tried to achieve it using an imageContext and blending the images together with no luck:
UIGraphicsBeginImageContext(CGSizeMake(sz.width, sz.height));
for (int i=0; i<30; i++) {
UIImage* img = [self.delegate requestImage:self at:i];
CGPoint coord = [self.delegate requestTranslation:self at:i];
[img drawAtPoint:coord blendMode:kCGBlendModeNormal alpha:1/30];
}
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
How could I get an averaged image of many UIimages?
I have also tried adding an image with many sublayers, but I also get washed out images.
Thanks!
Try changing the following:
[img drawAtPoint:coord blendMode:kCGBlendModeNormal alpha:1/30];
to
[img drawAtPoint:coord blendMode:kCGBlendModeNormal alpha:1.0/30.0];
1/30 (using integer values) == 0, so you'll be drawing the images completely transparent. By adding the .0, you clarify that you want a CGFloat.

How to display two UIImages one over another

i have to display two images one below the other. The images should look as if one is overlapping another(more over like a 3d image). i am using(i must use) drawRect method to display the images. i am even putting a code snippet that im using... Ca anyone guide me regarding this. Your inputs would help me go a long way.. Thank you.
*Here coverRect contains an image and UIImage *s is also a image...
if (columnIndex == 1) {
coverRect = CGRectMake(41,77 ,120 ,150 );
textRect = CGRectMake(31, 190 ,120 ,15 );
if (rowIndex != 0 && currentlyInEditingMode == NO) {
UIImage *s = [UIImage imageNamed:#"tray_center.png"];
[s drawInRect:CGRectMake(0, 0, s.size.width, s.size.height)];
}
}
Use the following API,
drawInRect:(CGRect)rect blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha
and for the above image set the alpha value according the opacity value you require. And set the 1.0 as the alpha value for the below image.

Computing a UIImage to be saved to the photo album

I basically want to automatically create a tiled image from a bunch of source images, and then save that to the user's photo album. I'm not having any success drawing a bunch of small UIImage's into one big UIImage. What's the best way to accomplish this? Currently I'm using UIGraphicsBeginImageContext() and [UIImage drawAtPoint], etc. All I ever end up with is a 512x512 black square. How should I be doing this? I'm looking to CGLayer's, etc. seems there are a lot of options but none that work particularly easily.
Let me actually put my code in:
CGSize size = CGSizeMake(512, 512);
UIGraphicsBeginImageContext(size);
UIGraphicsPushContext(UIGraphicsGetCurrentContext());
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
UIImage *image = [self getImageAt:i:j];
[image drawAtPoint:CGPointMake(i*128,j*128)];
}
}
UIGraphicsPopContext();
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(result, nil, nil, nil);
I should note that actually the above is not what happens in my code exactly. What really happens is that I call every line before and including to UIGraphicsPushContext, then in an animation timer I slowly increment the drawing and draw to the context. Then after it's all done I call everything from UIGraphicsPopContext onward.
Oh, then you can just save the onscreen view after it has been rendered on screen:
UIGraphicsBeginImageContext(myBigView.bounds.size);
[view drawRect:myBigView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
are you storing it back to an image?
UIImage *myBigImage = UIGraphicsGetImageFromCurrentImageContext();
To do exactly what I wanted to do...
Make your GLView as big as the total thing you want. Also make sure glOrtho and your viewport have the right size. Then just draw whatever you want wherever you want, and take a single OpenGL screenshot. Then you don't need to worry about combining to a single UIImage over multiple OpenGL rendering passes, which is no doubt what was causing my issue.

managing images in UIScrollView

i'm not understanding where in my code i write in the image names and in what order to appear. Here is my code
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
I have images that are titled "image0.jpeg, image1.jpg." how to i insert this into my code and order them in a certain way?
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
use %d for signed int
and %u for unsigned
Your code snippet is already doing what you want - at least partially.
If you have an image numbered 0, then you need to start your loop with i = 0 instea of i = 1, and adjust the constraint appropriately:
for (NSUInteger i = 0; i < kNumImages; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%u.jpg", i];
// create the image and insert into imageview
// add imageview to the containing view
}
The order is quite straight forward, as the images will be added 0, 1, 2.. and so forth
With the code from your first post, you create multiple (kNumImages, to be more specific) ImageViews, and load in them JPG files from your project directory, called "imageN.jpg", where N is integer between 1 and kNumImages.
To display these newly created views in your UIScrollView, you have to add them to it as subviews.
Something like
for (int i = 0; i < pageCount; i++) {
UIView *view = [pageViews objectAtIndex:i];
if (!view.superview)
[scrollView addSubview:view];
view.frame = CGRectMake(pageSize.width * i, 0, pageSize.width, pageSize.height);
}
The most straightforward way to do this is in UIScrollView's UIViewController. You may want to add them in some sort of collection (it's likely that the collection retains them, so don't forget to release the views, when you add them).
As you get more comfortable with your application, you may want to lazy-load the views or use simple UIViewControllers for the different images.
In iPhone OS 3.0 and above, there are significant improvements in UIScrollView, and you can find excellent code samples in the ScrollViewSuite tutorial project on ADC's iPhone section.