iOS Adding Dynamic Values to Array - ios

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).

Related

Move the object in the array

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

Image Iteration in iOS

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

iOS pass the value instead of reference

When executing the below block. My friends_dict NSMutableDictionary value changes as friend changes. If I'm not wrong this is because the reference of the value in the dictionary is passed along to the friend. How can I pass the value instead of reference and prevent the dictionary from changing.
NSMutableArray *friendsArray = [[NSMutableArray alloc] initWithArray:[friends_dict valueForKey:selectedFriendId]];
for (NSObject *object in [response objectForKey:#"friend_nearby"]){
NSString *id = [NSString stringWithFormat:#"%#",[object valueForKey:#"id"]];
NSString *spotValue = [NSString stringWithFormat:#"%#",[object valueForKey:#"spot"]];
for(int i = 0; i < [friendsArray count]; i++){
FriendDataModel *friend = [[FriendDataModel alloc] init];
friend = [friendsArray objectAtIndex:i];
if ([friend.friendId isEqualToString:id] && ![friend.spot isEqualToString:spotValue] ) {
friend.spot = spotValue; //This is where the dictionary value changes
[friendsArray replaceObjectAtIndex:i withObject:friend];
spotChanged = YES;
}
}
}
Copy the array and pass in the copy.
NSArray *friendList = [friends_dict valueForKey:selectedFriendId];
NSMutableArray *friendsArray = [friendList mutableCopy];

How do I convert an LDAP "jpegPhoto" to NSString to UIImageView

I am trying to pull an LDAP "jpegPhoto" attribute from an openLDAP server using a iOS openLDAP framework. The framework pulls the data as a dictionary of NSStrings.
I need to convert the NSString of "jpegPhoto" (which also appears to be base64 encoded) to UIImage, with the end result being that I display the jpegPhoto as the user's image when they login.
More Info:
-(NSDictionary *)doQuery:(NSString *)query:(NSArray *)attrsToReturn {
...
while(attribute){
if ((vals = ldap_get_values_len(ld, entry, attribute))){
for(int i = 0; vals[i]; i++){
//Uncomment if you want to see all the values.
//NSLog(#"%s: %s", attribute, vals[i]->bv_val);
if ([resultSet objectForKey:[NSString stringWithFormat:#"%s",attribute]] == nil){
[resultSet setObject:[NSArray arrayWithObject:[NSString stringWithFormat:#"%s",vals[i]->bv_val]] forKey:[NSString stringWithFormat:#"%s",attribute]];
}else{
NSMutableArray *array = [[resultSet objectForKey:[NSString stringWithFormat:#"%s",attribute]] mutableCopy];
[array addObject:[NSString stringWithFormat:#"%s",vals[i]->bv_val]];
[resultSet setObject:array forKey:[NSString stringWithFormat:#"%s",attribute]];
}
}
ldap_value_free_len(vals);
};
ldap_memfree(attribute);
attribute = ldap_next_attribute(ld, entry, ber);
};
...
}
-(UIIMage *)getPhoto{
NSString *query = [NSString stringWithFormat:#"(uid=%#)",self.bindUsername];
NSArray *attrsToReturn = [NSArray arrayWithObjects:#"cn",#"jpegPhoto", nil];
NSDictionary *rs = [self doQuery:query:attrsToReturn];
NSString *photoString = [[rs objectForKey:#"jpegPhoto"] objectAtIndex:0];
NSLog(#"The photoString is: %i %#",[photoString length],#"characters long"); //returns 4
NSData *photoData = [NSData dataWithBase64EncodedString:photoString];
UIImage *userPhoto = [UIImage imageWithData:photoData];
return userPhoto;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.studentNameLabel.text = [NSString stringWithFormat:#"Hi %#!",[self.ldap getFullName]];
self.studentPhotoImage.image = [self.ldap getPhoto];
[self checkForProctor];
}
Try this code
NSData *dataObj = [NSData dataWithBase64EncodedString:beforeStringImage];
UIImage *beforeImage = [UIImage imageWithData:dataObj];
There are many similar questions in Stackoverflow.. Please refer the following links
UIImage to base64 String Encoding
UIImage from bytes held in NSString
(Since there has been no working code posted for getting the image data from LDAP, I wanted to add this answer for the benefit of future visitors.)
The missing piece was reading the binary data into an NSData object rather than an NSString when you have binary data that might contain NULL (zero) values within it, such as images or GUIDs.
value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len];
+ (NSArray *)searchWithBaseDN:(const char *)baseDN andFilter:(const char *)filter andScope:(int)scope {
...
while(entry)
{
// create a dictionary to hold attributes
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
attribute = ldap_first_attribute(ld, entry, &ber);
while(attribute)
{
if ((vals = ldap_get_values_len(ld, entry, attribute)))
{
if (ldap_count_values_len(vals) > 1) {
NSMutableArray *values = [[NSMutableArray alloc] init];
for(int i = 0; vals[i]; i++) {
[values addObject:[NSString stringWithUTF8String:vals[i]->bv_val]];
}
[dictionary setObject:values forKey:[NSString stringWithUTF8String:attribute]];
} else {
NSObject *value = nil;
if (strcmp(attribute, "thumbnailPhoto") == 0 || strcmp(attribute, "objectGUID") == 0) {
value = [NSData dataWithBytes:vals[0]->bv_val length:vals[0]->bv_len];
} else {
value = [NSString stringWithFormat:#"%s", vals[0]->bv_val];
}
[dictionary setObject:value forKey:[NSString stringWithUTF8String:attribute]];
}
ldap_value_free_len(vals);
};
ldap_memfree(attribute);
attribute = ldap_next_attribute(ld, entry, ber);
};
...
}

How do I use imageWithContentsOfFile for an array of images used in an animation?

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.

Resources