table view displaying image which is in project - ios

i am trying to add image which i added in project i use following code for it
UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 7, 320, 50)];
[selectionView setBackgroundColor: [UIColor clearColor]];
UIImage *image = [[UIImage alloc]init];
image = [UIImage imageWithContentsOfFile:#"PlaceHolder.png"];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
blockView.frame = CGRectMake(0, -5, 45, 45);
if([[[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]] isEqualToString:#"No Image"] )
{
[selectionView addSubview:blockView];
}
else
{
NSString *path = [[_arrayForTable valueForKey:#"URL"] objectAtIndex:[indexPath row]];
NSURL *url = [NSURL URLWithString:path];
AsyncImageView *imageView = [[[AsyncImageView alloc] init] autorelease];
imageView.frame = CGRectMake(0, -5, 45, 45);
imageView.imageURL=url;
[selectionView addSubview:imageView];
}
else condition work perfectly they have url of facebook to display images in table view but if condition in not working it going in to if condition but image is not getting displayed plz help

UIImage *image = [UIImage imageNamed:#"PlaceHolder.png"];
UIImageView* blockView = [[UIImageView alloc] initWithImage:image];
/*
imageNamed: is to load the picture which is in bundle. pass in the picture name
imageWithContentsOfFile: need to pass in the whole address of the file
*/

Try
UIImage *image = [UIImage imageNamed:#"yourfile.png"];
and add the image to the imageView and check the image file is aded to your project

Related

Scale UIImage to fit UIImageView correctly

I have following code that takes an image form photo gallery using UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickerImg = info[UIImagePickerControllerEditedImage];
if (pickerImg == nil) {
pickerImg = info[UIImagePickerControllerOriginalImage];
}
NSData *imageData = UIImagePNGRepresentation(pickerImg);
UIImage *img = [UIImage imageWithData:imageData];
UIImage *fixedOrientationImage = [UIImage imageWithCGImage:img.CGImage scale:pickerImg.scale orientation:pickerImg.imageOrientation];
pickerImg = fixedOrientationImage;
cropView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
cropView.backgroundColor = [UIColor darkTextColor];
[self.navigationController setNavigationBarHidden:YES animated:YES];
UIImageView * cropImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 60)];
cropImageView.image = pickerImg;
cropImageView.contentMode = UIViewContentModeScaleAspectFit;
cropImageView.clipsToBounds = YES;
[cropView addSubview:cropImageView];
[self.view addSubview:cropView];
}
I have selected the following image
that is shown over above cropImageView like this
Try setting either the image or the frame after setting the content mode
UIImageView * cropImageView = [[UIImageView alloc] init];
cropImageView.contentMode = UIViewContentModeScaleAspectFit;
cropImageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 60);
cropImageView.image = pickerImg;
cropImageView.clipsToBounds = YES;

UIImage animatedImageNamed repeat count

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];

ios How to add UIImage to UITextView asynchronous?

I can add image asynchronously seperately by using UIImageView+AFNetworking
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
I can also add image to UITextView by
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImage:[UIImage imageNamed:#"image.png"]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
But I cannot add image asynchronously in UITextView like
UITextView *text = [[UITextView alloc] init];
UIImageView *imageView = [UIImageView new];
[imageView setImageWithURL:[NSURL URLWithString:[self.data valueForKey:#"image_link"]]];
CGRect aRect = CGRectMake(0, 0, 125, 120);
[imageView setFrame:aRect];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:aRect];
text.textContainer.exclusionPaths = #[exclusionPath];
[text addSubview:imageView];
So how can I add image asynchronously in UITextView?
Use a dispatch :
dispatch_async(dispatch_get_main_queue(), ^{
// async UI update here
});

How would I use a string of image names to use in a UIImageView?

I keep ending with an error saying that there are incompatible pointers assigning UIIMAGE to NSString.
self.imageNames = [NSArray arrayWithObjects:#"shop_deal.png",#"Date.png",#"Recreation.png",#"Productivity.png",nil];
UIImageView *imgView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 150)];
imgView.image = [UIImage imageNamed:_imageNames];
[self addSubview:imgView];
I'm doing something wrong here...
In this line:
imgView.image = [UIImage imageNamed:_imageNames];
you have to use an NSString, not an entire NSArray. So try:
imgView.image = [UIImage imageNamed:[_imageNames objectAtIndex:0]];
instead.

how to place different UIImages

Depending on the result of some user input, i have to show from 0 to n number of images [lines in a chart style background] in a
view, changing on the x axis,
the problem is that i dont know before hand how many images are going to be needed to draw,
so i cannot create all the UIImageViews needed [nor is elegant coding]
here an example of what I mean
UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(12, 30, 12, 200)] autorelease];
[img setImage:[UIImage imageNamed:#"line.png"]];
img.opaque = YES;
[self.view addSubview:img];
//[img release];
UIImageView *img2 = [[[UIImageView alloc] initWithFrame:CGRectMake(102, 30, 12, 200)] autorelease];
[img2 setImage:[UIImage imageNamed:#"line.png"]];
img2.opaque = YES;
[self.view addSubview:img2];
UIImageView *img3 = [[[UIImageView alloc] initWithFrame:CGRectMake(202, 30, 12, 200)] autorelease];
[img3 setImage:[UIImage imageNamed:#"line.png"]];
img3.opaque = YES;
[self.view addSubview:img3];
UIImageView *img4 = [[[UIImageView alloc] initWithFrame:CGRectMake(302, 30, 12, 200)] autorelease];
[img4 setImage:[UIImage imageNamed:#"line.png"]];
img4.opaque = YES;
[self.view addSubview:img4];
so how to accomplish this?
thanks a lot!
If in height you have height of your view, than:
for (int x=2; x<height; x+=100)
{
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 30, 12, 200)];
[img setImage:[UIImage imageNamed:#"line.png"]];
img.opaque = YES;
[self.view addSubview:img];
[img release];
}

Resources