How to move objects in this array? Tried everything. I cannot select an individual object.
self.images = [NSMutableArray array];
NSFileManager* manager = [NSFileManager new];
NSBundle* bundle = [NSBundle mainBundle];
NSDirectoryEnumerator* enumerator = [manager enumeratorAtPath:[bundle bundlePath]];
for (NSString* name in enumerator) {
if ([name hasSuffix:#"PawnWhite.png"]) {
for (int i = 0; i <= 7; i++) {
UIImage* image = [UIImage imageNamed:name];
[self.images addObject:image];
}
}
}
for (int i = 0; i < self.images.count; i++) {
CGPoint myPoint = CGPointMake(75.f, 0);
self.view = [[UIView alloc] initWithFrame:CGRectMake(84.f + myPoint.x * i, 870.f, 75.f, 75.f)];
self.imagesView = [[UIImageView alloc] initWithFrame:self.view.bounds];
self.imagesView.image = [self.images objectAtIndex:i];
[self.view addSubview:self.imagesView];
[valueView addSubview:self.view];
}
I have pictures in the array, mapped on main view. I need to change any of them that she had changed the location.
If you want to move objects around within an array - you just remove an item from one location, and add it to another, like this :
[self.images removeObjectAtIndex:index];
[self.images insertObject:object atIndex:newIndex];
Related
Is there a better way to iterate the following images?
NSArray *imageNames = #[#"MyImage-0.png", #"MyImage-1.png", #"MyImage-2.png",#"MyImage-3.png",#"MyImage-4.png", #"MyImage-5.png", #"MyImage-6.png", #"MyImage-7.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
mImageView.animationImages = images;
mImageView.animationDuration = 1.5;
[self.view mImageView];
[mImageView startAnimating];
NSInteger imagesCount = 7;
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i <= imagesCount; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat:#"MyImage-%d", i]]];
}
mImageView.animationImages = images;
mImageView.animationDuration = 1.5;
[self.view mImageView];
[mImageView startAnimating];
You can use enumeration
for(NSString *imageStr in imageNames)
{
[images addObject:[UIImage imageNamed:imageStr]];
}
This takes less time to iterate.
Hope it helps you...!
The better way will be not using for loop. Try this code
+(NSArray*)getSortedArray:(NSMutableArray*)arrayToSort
{
return [arrayToSort sortedArrayUsingSelector:#selector(compare:)];
}
Hope this class method will help you.
You might want to use some handy library, like this one. Look, it even supports GIF!
NSString *urlString = #"http://raphaelschaad.com/static/nyan.gif";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:data];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0);
[self.view addSubview:imageView];
I want to add dynamic values of UIImage in array which initialised in the for loop, So I created a NSMutableArray to set object first in for loop and later tried to retrieve values from that.
Please can you look where I am going wrong and what should be best approch while adding the dynamic values.
NSMutableDictionary * dic= [[NSMutableDictionary alloc]init];
NSMutableArray *cImages = [[NSMutableArray alloc]init];
for(int i=0; i<5; i++) {
NSString * imageKey= [NSString stringWithFormat:#"image%i",i+1];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [[results objectAtIndex:i]valueForKey:imageKey]]];
UIImage *imageOne = [UIImage imageWithData:imageData];
[dic setObject:imageOne forKey:imageKey];
}
for (NSString *key in dic) {
NSString *sprite = [dic objectForKey:key];
//adding below to the array
[cImages addObject:sprite];
}
What's wrong with just adding UIImage's to the mutable array?
NSMutableArray *cImages = [[NSMutableArray alloc]init];
for(int i = 0; i < 5; i++)
{
NSString *imageKey= [NSString stringWithFormat:#"image%i", i+1];
UIImage *imageToAdd = [UIImage imageNamed:imageKey];
[cImages addObject:imageToAdd];
}
// Using the images
for (UIImage *image in cImages)
{
// Do whatever u want with each image.
}
If you really need to use a dictionary to access the UIImage's by the names "image1, image2" etc you can do this:
NSDictionary *dict = [[NSDictionary alloc] init];
for (int i = 0; i < 5; i++)
{
NSString *imageKey = [NSString stringWithFormat:#"image%i", i+1];
UIImage *image = [UIImage imageNamed:#"nameOfImageFile.png"];
[dict setObject:image forKey:imageKey];
}
// Now you can access the UIImage values by their names (image1,image2,image3,image4,image5)
UIImage *imageToUse = [dict valueForKey:#"image1"];
(haven't test these, but it's fairly standard).
I have an image slideshow with images that are being parsed with JSON and I want to run that slideshow in a background thread.
My original code:
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages we want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Load an individual page, first seeing if we've already loaded it
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *images = _singleRelease[#"images"];
NSMutableArray *mediumImages = [NSMutableArray array];
for (NSDictionary *imageDictionary in images){
NSURL *imageURL = [NSURL URLWithString:imageDictionary[#"image_file"][#"image_file"][#"medium"][#"url"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
[mediumImages addObject:image];
}
self.pageImages = [mediumImages copy];
NSInteger pageCount = self.pageImages.count;
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 4
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// 5
[self loadVisiblePages];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages which are now on screen
[self loadVisiblePages];
}
I tried customizing a code sample I found online, but I cant get it to work with my code.
This is what I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *images = _singleRelease[#"images"];
NSMutableArray *mediumImages = [NSMutableArray array];
dispatch_queue_t imageQueue = dispatch_queue_create("Image Queue",NULL);
for (NSDictionary *imageDictionary in images){
dispatch_async(imageQueue, ^{
NSURL *imageURL = [NSURL URLWithString:imageDictionary[#"image_file"][#"image_file"][#"medium"][#"url"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
[mediumImages addObject:image];
dispatch_async(dispatch_get_main_queue(), ^{
NSInteger pageCount = self.pageImages.count;
self.pageImages = [mediumImages copy];
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
});
});
}
}
This was another code sample I tried customizing but I get the same outcome.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *images = [self.singleRelease[#"images"] copy];
__weak __typeof__(self) weakself = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *mediumImages = [NSMutableArray array];
for (NSDictionary *imageDictionary in images){
NSURL *imageURL = [NSURL URLWithString:imageDictionary[#"image_file"][#"image_file"][#"medium"][#"url"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
if (image) {
[mediumImages addObject:image];
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
if (weakself) {
__typeof__(self) weakself = weakself;
self.pageImages = mediumImages;
NSInteger pageCount = self.pageImages.count;
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
};
});
});
}
Both codes load my Page Control with the correct number of slides, but the images don't appear.
Any help? Thanks.
Fixed it.
Delete viewWillAppear and then replace the current viewDidLoad with this:
/**
* Called after the controller’s view is loaded into memory.
*/
- (void)viewDidLoad
{
[super viewDidLoad];
self.release_name.text = [self.singleRelease objectForKey:#"release_name"];
if ([_singleRelease objectForKey:#"release_price"])
self.release_price.text = [NSString stringWithFormat:#"$%#",[_singleRelease objectForKey:#"release_price"]];
self.release_colorway.text = [self.singleRelease objectForKey:#"release_colorway"];
if([_singleRelease objectForKey:#"release_date"] != NULL)
{
NSString *readableDate = [_singleRelease objectForKey:#"release_date"]; // I assume that this is a string
UpcomingRelease *upcoming = [[UpcomingRelease alloc] init];
upcoming.release_date = readableDate;
self.release_date.text = [NSString stringWithFormat:#"%#", upcoming.formattedDate];
}
NSArray *images = self.singleRelease[#"images"];
// we get the image on a background queue
dispatch_queue_t imageQueue = dispatch_queue_create("Image Queue",NULL);
dispatch_async(imageQueue,
^{
NSMutableArray *mediumImages = [[NSMutableArray alloc] initWithCapacity:images.count];
// for each image url we get the image and add it to the array
for (NSDictionary *imageDictionary in images)
{
NSURL *imageURL = [[NSURL alloc] initWithString:imageDictionary[#"image_file"][#"image_file"][#"medium"][#"url"]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[mediumImages addObject:image];
}
// once we have obtained all of the images we use them
dispatch_async(dispatch_get_main_queue(),
^{
// we get a strong pointer to the images array and set the pages count according to how many there are
self.pageImages = [mediumImages copy];
NSInteger pageCount = self.pageImages.count;
// we set the page control appropriate for the number of images that need to be presented
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// we then make mock page views
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i)
{
[self.pageViews addObject:[NSNull null]];
}
// once we have the images we load the pages with them
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
[self loadVisiblePages];
});
});
_scrollView.showsHorizontalScrollIndicator=NO;
_scrollView.showsVerticalScrollIndicator=NO;
}
The problem was you were trying to load the pages before the images had finished fetching.
I have a UIImageView on my storyboard, which is linked to an outlet in the code. When I run the code, it only shows the static picture that the storyboard shows, not the animation that I create programmatically. Any ideas?
for(int i=0; i<4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"loading%d",i]];
[self.images addObject:image];
}
self.loadingView.animationImages = self.images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Note: loadingView is the UIImageView, images is an NSMutableArray.
If you want to make run time array you can use like this way.
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 1 ; i < 4; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat: #"loading%d.jpg", i]]];
}
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
Hope this will help you. It's working fine for me.
I recommend you to use array directly like this.
NSArray *images = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.jpg"],
[UIImage imageNamed:#"2.jpg"],
nil];
self.loadingView.animationImages = images;
self.loadingView.animationDuration = 2;
self.loadingView.animationRepeatCount = 1;
[self.loadingView startAnimating];
This is what I currently have:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [NSString stringWithFormat:#"dance2_%03d.jpg", count];
UIImage *frame = [UIImage imageNamed:fileName];
[images addObject:frame];
}
UIImage imageNamed is causing me some memory issues and I would like to switch to imageWithContentsOfFile.
I can make it work with a single image, but not the whole array:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/dance2_001.jpg"];
UIImage *frame = [UIImage imageWithContentsOfFile:fileName];
[images addObject:frame];
}
Any help is greatly appreciated! Thanks!
for(int i = 1; i <= 21; i++)
{
[images addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"dance2_%03d", i] ofType:#"jpg"]]];
}
what you should do first is create an array with the images for your animation like something like this:
NSMutableArray* images = [[NSMutableArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image1" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image2" ofType:#"jpg"]],
nil];
then you can add it to an UIImageView to animate it like this:
UIImageView* animationImagesView = [[UIImageView alloc] initWithFrame:CGRectMake(posX, posY, frameWidth, frameHeight)];
animationImagesView.animationImages = images; //array of images to be animate
animationImagesView.animationDuration = 1.0; //duration of animation
animationImagesView.animationRepeatCount = 1; //number of time to repeat animation
[self.view addSubview:animationImagesView];
now you can start and stop the animation using these two calls:
[animationImagesView startAnimating]; //starts animation
[animationImagesView stopAnimating]; //stops animation
hope this helps. also remember to release and nil your Array and UIImageView when done.