I am rather new to Cocoa programming and need some help.
I want to make some image views for units in a game and add them with code, because to put them in the view and make conections in Interface Builder is a lot of work.
I already have found code to create and insert UIImageViews:
UIImageView *image0 =[[UIImageView alloc] initWithFrame:CGRecMake(x,y,w,h)];
image0.image=[UIImage imageNamed:#"image.png"];
[self.view addSubview:image0];
My question is: if I can make an array, then I don't have to write this for every image view, and I put the same image in it?
A nice way to do it would be something like this. This will create 30 image views with the same image.
NSMutableArray *arrayOfImageViews = [NSMutableArray array];
for (int i = 0; i < 30; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"theImage.png"]];
[self.view addSubview:imageView];
[arrayOfImageViews addObject:imageView];
}
How about this:
for(int i=0; i<10; ++i) {
UIImageView *image =[[UIImageView alloc] initWithFrame:CGRecMake(x+i*k1,y*i*k2,w,h)];
image.image=[UIImage imageNamed:#"image.png"];
image.tag = i+1;
[self.view addSubview:image0];
}
Related
I used MMScrollPresenter for 4 images. It is perfectly done. But now i want to used this for dynamic, how can i done. I know this can be done using for loop.
NSArray *array = [[NSArray alloc]initWithObjects:#"mountains.jpg",#"forest.jpg", nil];
for(int i=0;i<array.count;i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",array[i]]]];
[imageView setFrame:CGRectMake(0, 0, scrollImg.frame.size.width, scrollImg.frame.size.height)];
Page = [[MMScrollPage alloc] init];
[Page.backgroundView addSubview:imageView];
}
[scrollImg addMMScrollPageArray:#[Page]];
After using this code, scroll view not working. Anybody can help me.
I have a images sequence animated with :
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
[UIImage animatedImageNamed:#"spinner-" duration:1.0f];
imgView.contentMode = UIViewContentModeCenter;
imgView.animationRepeatCount = 1; // No effect
[self.view addSubView:imgView];
But the sequence keeps looping. Is there a way to control the repeat count (and keep displaying the last image) ?
In your case, using [UIImage animatedImageNamed:#"spinner-" duration:1.0f]; won't give you what you want.
For what you would like to achieve, I would recommend using UIImageView.
Usage:
First create an UIImageView object:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:<FRAME_SIZE>];
Second, create an array that holds your images to be animated.
If you just have a few, do the following:
NSMutableArray *animatedImagesArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"spinner-1.png"], [UIImage imageNamed:#"spinner-2.png"], nil];
If you have more, then you could load your images into the array like this:
for (int i = 0; i < <NUM_OF_IMAGE_COUNT>; i++)
{
[animatedImagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"spinner-%d%#", i, #".png"]]];
}
Once all that is done, then we configure the controls you want:
imageView.animationImages = animatedImagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 1.0f;
Then start animating it by:
[imageView startAnimating];
And stop the animation via:
[imageView stopAnimating];
If you would like the UIImageView to hold the last image after stopping, we would need to carry out the following before starting the animation:
imageView.image = [imageView.animationImages lastObject];
....
[imageView startAnimating];
Hope this helps.
You set the repeat count in the UIImageView object that displays your image.
UIImage *testImage = [UIImage animatedImageNamed:#"Box.jpg" duration:1.0f];
UIImageView *imgView = [[UIImageView alloc] initWithImage:testImage];
[imgView setAnimationRepeatCount:1];
i'm creating a app where i'm adding some UIImages as subview of UIview. Now if I delete image I want to rearrange the remaining UIImages.
How can I achieve this?
EDIT
This is what i have tried so far:
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setTag:i+1];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
Add You all Images in NSMutableArray and declare NSMutableArray in .h file.
self.imagArray = [[NSMutableArray alloc] init];
[self.imagArray addObject:[UIImage imageWithName:#"image1.png"];
.
.
.
.
[self.imagArray addObject:[UIImage imageWithName:#"image_N.png"];
And you can easily manage it for add/delete image by self.imagArray.
If you want to delete any image then also write this code
[self.imagArray removeObjectsAtIndexes:indexes];
I assume you are using array of images. Just remove the index once it's deleted and have a login to refresh the view.
Init images to array first:
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
Delete image:
NSUInteger deleteIdx = [ImagesArray indexOfObject:deleteImage];
[deleteImage removeFromSuperview];
[self relayoutViews:deleteIdx];
- (void)relayoutViews:(int)deleteIdx{
for (int i=deleteIdx; i<[ImagesArray count]; i++) {
UIImageView *imageView = [ImagesArray objectAtIndex:i];
imageView.frame = CGRectMake(20, 100*i+100, 60, 60)];
}
}
With the help of tag you can get the imageview which user want to delete .So you have to add TapGesture on UIimageView and you have to bind method for deleting image.Like this
for (int i=0; i<[array count]; i++)
{
id dict = [array objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100*i+100, 60, 60)];
[imageView setBackgroundColor:[UIColor clearColor]];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(oneTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[imageView addGestureRecognizer:singleTap];
[imageView setTag:i+1];
[ImagesArray addObject:imageView];
[self.view addSubView: imageView];
}
- (void)oneTap:(UIGestureRecognizer *)gesture {
int myViewTag = gesture.view.tag;
// Now with the help of tag you can remove object from you array and which you are want to remove and then you can reload you view
}
I want to add 3 images from 3 URLs in ScrollView so how could I do?
Please give me a good tutorial or some explanations!
You can use "AsyncImageView" class files it will load image synchronically.
please refer
https://stackoverflow.com/a/15377082/1713478
in this answer this method call for tableview you can use in for loop and place imageview in scrollview
for putting images vertically
int y = 10;
for (int i = 0; i < (number of images); i++)
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(X, y, width, height)];
NSString *imageURL = [NSString stringWithFormat: (image link for i number)];
AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
[async loadImageFromURL:[NSURL URLWithString:imageURL]];
[imageView addSubview:async];
[scrollView addSubview:imageView];
y = y+height+10;
}
You can user PSTCollectionView for gridView kind of display and SDWebImage for lazy loading.
Are you asking how to load remote images OR how to layout them on a scroll view?
1.
UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"..."]]];
Wrap it all in dispatch_async or put it in NSOperation to make sure you don't block the main thread.
2.
You can try to use UIPageViewController instead of a UIScrollView
I need to change the pictures into animation video, but I don't known how to make a video, do not know to have what good method?
for example make your image names as xxxx-1.png, xxxx-2.png ...... xxxx-80.png. and paste the below code.. then the images will animate like video.
NSMutableArray* imgArray = [[NSMutableArray alloc] init];
for (int i=1; i <= 80; i++) {
[imgArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"xxxx-%d.png",i]]];
}
UIImageView* imgView = [UIImageView alloc] initWithFrame:(x,y,a,b)];
imgView.animationImages = imgArray;
imgView.animationDuration = 0.25;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
[self.view addSubview:imgView];
[imgView release];