iOS UIImageView animationImages display remote pictures - uiimageview

I have three urls just like "http://xxx.x.xx.xx:8180/admin/upload/advert/advert_img.jpg",
they are pictures in the remote server.
And I have an UIImageView, I want to use:
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 504.0, 320.0, 40.0)];
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"advert_img"],[UIImage imageNamed:#"img01"],[UIImage imageNamed:#"img02"], nil];
_imageView.animationImages = images;
_imageView.animationDuration = 15;
[_imageView startAnimating];
to display these pictures. How can I do this? Thank you very much.

Try this :
NSArray *frames = [NSArray arrayWithObjects:
[UIImage imageWithName:#"advert_img.png"],
[UIImage imageWithName:#"img01.png"],
[UIImage imageWithName:#"img02.png"],
nil];
UIImageView *animatedIMvw = [[UIImageView alloc] init];
animatedIMvw.animationImages = frames;
[animatedIMvw startAnimating];

Related

Capture image from video file URL

-(void)captureImageFromUrlPath : (NSString *)urlStr {
// Import this from for these classes #import <AVFoundation/AVFoundation.h>
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:urlStr] options:nil];
AVAssetImageGenerator* imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
UIImage* image = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:CMTimeMake(0, 1) actualTime:nil error:nil]];
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImage:image];
[imageView setFrame:CGRectMake(0, 20, 320, 200)];
[self.view addSubview:imageView];
}
This is the best way to capture image from video file Url.

UIImageView animating pictures

How do I cycle the images in an UIImageView?
Say I have three images, 1.jpg, 2.jpg, 3.jpg. How do I get the pictures to change ad infinitum until the user presses something...
This describes it pretty well: [Link]
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"campFire01.gif"],
[UIImage imageNamed:#"campFire02.gif"],
[UIImage imageNamed:#"campFire03.gif"],
[UIImage imageNamed:#"campFire04.gif"],
[UIImage imageNamed:#"campFire05.gif"],
[UIImage imageNamed:#"campFire06.gif"],
[UIImage imageNamed:#"campFire07.gif"],
[UIImage imageNamed:#"campFire08.gif"],
[UIImage imageNamed:#"campFire09.gif"],
[UIImage imageNamed:#"campFire10.gif"],
[UIImage imageNamed:#"campFire11.gif"],
[UIImage imageNamed:#"campFire12.gif"],
[UIImage imageNamed:#"campFire13.gif"],
[UIImage imageNamed:#"campFire14.gif"],
[UIImage imageNamed:#"campFire15.gif"],
[UIImage imageNamed:#"campFire16.gif"],
[UIImage imageNamed:#"campFire17.gif"], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];

xcode: segmented control appears on sim, not on device

I have an iPad app with several segmented controls, including one view with two controls.
On that view, everything shows in the sim, both normal and retina. However, in the device, only one shows.
Below is the code that does not show on the device.. I have checked and all the images that make up the control are copied in the bundle resources. I've tried removing, cleaning, etc. No joy. I must be missing something (hopefully simple)..
UISegmentedControl *controls = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
nil]];
CGRect frame = CGRectMake(35, 70, 700, 35);
controls.frame = frame;
}
[controls addTarget:self action:#selector(drawSegmentAction:) forControlEvents:UIControlEventValueChanged];
controls.segmentedControlStyle = UISegmentedControlStyleBar;
controls.momentary = YES;
controls.tintColor = [UIColor grayColor];
[self.view addSubview:controls];
}
For reference, this code in the same view does work:
For reference, this control code does work:
-(void) buildColorBar {
//NSLog(#"%s", __FUNCTION__);
UISegmentedControl *colorControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects: [UIImage imageNamed:#"White.png"],
[UIImage imageNamed:#"Red.png"],
[UIImage imageNamed:#"Yellow.png"],
[UIImage imageNamed:#"Green.png"],
[UIImage imageNamed:#"Blue.png"],
[UIImage imageNamed:#"Purple.png"],
[UIImage imageNamed:#"Black.png"],
nil]];
NSLog(#"Portrait");
CGRect frame = CGRectMake(35, 950, 700, 35);
colorControl.frame = frame;
// When the user chooses a color, the method changeBrushColor: is called.
[colorControl addTarget:self action:#selector(changeBrushColor:) forControlEvents:UIControlEventValueChanged];
colorControl.segmentedControlStyle = UISegmentedControlStyleBar;
// Make sure the color of the color complements the black background
colorControl.tintColor = [UIColor grayColor];
// Add the control to the window
[self.view addSubview:colorControl];
}
Is there a rule against using two segmented controls in one view?
I found that one image in the segmented control - while it looked like it was in the bundle - it wasn't. PITA, but at least, it works..

How to add a jpg or png image to an UIImage and then display in a UIImageView?

UIImage *img = [[UIImage alloc] initWithContentsOfFile:#"contactHeader.png"];
_headersView = [[UIImageView alloc] initWithImage:img];
I have already hooked up the connections for the UIImageView (_headersView) in IB. The image I want to load is in my project tree structure as well.
Am I creating the UIImage the right way?
If the file is in your bundle you can just use
UIImage *img = [UIImage imageNamed:#"contactHeader"];
_headersView = [[UIImageView alloc] initWithImage:img];
If the image is in your documents directory you can use
// Get the path to your documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Add the file to the end of the documents path
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"contactHeader.png"];
UIImage *img = [[UIImage alloc] imageWithContentsOfFile:filePath];
_headersView = [[UIImageView alloc] initWithImage:img];
[img release]; img = nil;
try this
-(NSString *)getPath:(NSString *)filename
{
self.path = [[NSBundle mainBundle]pathForResource:filename ofType:#"png"];
return path;
path = nil;
}
self.youImageView.image = [UIImage imageWithContentsOfFile:[self getPath:#"123"]];

xcode iphone - jerky scroll UITableView CellForRowAtIndexPath

Almost sorted with my 1st app, just a simple news app but when I load it onto my iPhone the scroll seems jerky can someone have a look at my function and see if i'm doing something wrong.
I need the image on the right hand side thats why i'm using custom cells.
Thanks
For any help
#define DATELABEL_TAG 1 #define MAINLABEL_TAG 2 #define PHOTO_TAG 3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MainNewsCellIdentifier = #"MainNewsCellIdentifier";
UILabel *mainLabel, *dateLabel;
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MainNewsCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MainNewsCellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,15.0,170.0,15.0)] autorelease];
dateLabel.tag = DATELABEL_TAG;
dateLabel.font = [UIFont systemFontOfSize:10.0];
dateLabel.textAlignment = UITextAlignmentLeft;
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; //| UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:dateLabel];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,28.0,170.0,60.0)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont boldSystemFontOfSize:14.0];
mainLabel.textColor = [UIColor blackColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
mainLabel.numberOfLines = 0;
//mainLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:mainLabel];
photo = [[[UIImageView alloc] initWithFrame:CGRectMake(190.0,15.0,85.0,85.0)] autorelease];
photo.tag = PHOTO_TAG;
photo.contentMode = UIViewContentModeScaleAspectFit;//UIViewContentModeScaleAspectFit; //
[cell.contentView addSubview:photo];
}
else {
dateLabel = (UILabel *)[cell.contentView viewWithTag:DATELABEL_TAG];
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG];
}
NSUInteger row = [indexPath row];
NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row];
NSString *title = [stream valueForKey:#"title"];
NSString *titleString = #"";
if( ! [title isKindOfClass:[NSString class]] )
{
titleString = #"";
}
else
{
titleString = title;
}
CGSize maximumSize = CGSizeMake(180, 9999);
UIFont *dateFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize dateStringSize = [titleString sizeWithFont:dateFont
constrainedToSize:maximumSize
lineBreakMode:mainLabel.lineBreakMode];
CGRect dateFrame = CGRectMake(15.0, 28.0, 170.0, dateStringSize.height);
mainLabel.frame = dateFrame;
mainLabel.text = titleString;
dateLabel.text = [stream valueForKey:#"created"];
NSString *i = [NSString stringWithFormat:#"http://www.website.co.uk/images/%#", [stream valueForKey:#"image"]];
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
photo.image = newsImage;
[imageURL release];
[newsImage release];
return cell;
}
The problem is this:
NSString *i = [NSString stringWithFormat:#"http://www.website.co.uk/images/%#", [stream valueForKey:#"image"]];
NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]];
UIImage *newsImage = [[UIImage alloc] initWithData:imageURL];
You effectively say here, that as soon as the cell needs to be displayed, the image must be fetched and presented. This will cost some time - too much for a good user experience.
You should fetch the images before or while you present the table view, and cache them, e.g. in an array. Or you must handle things asynchronously, meaning that you do the loading in the background, and not wait with return cell; until the image is actually downloaded. This will be a little harder to get right.
Even if you asynchronously download images, you'll still have a jerky scroll.
Why? It's lazy image decompression. ios performs decompression at the moment it will be displayed on the screen. You have to manually decompress the images in a background thread.
Decompression does not merely mean instantiating a UIImage object. It can be somewhat complicated. The best solution, is to download SDWebImage and use the image decompressor that's included. SDWebImage will asynchronously download and perform decompression for you.
To read more about the issue see: http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/

Resources