I am trying to create an app like this:
(source: topmobiletrends.com)
where each of the views show an image for every in a photo in a JSON array.
I have successfully set and downloaded the array of images and set them in a UIImageView with with NSURLRequest and SDWebImage.
I created a UIView with an image view inside to test the code in my ViewController(Storyboard), everything works fine. However, when I try to create a UIView to loop programmatically, it shows up blank, with no ImageView. I'm not sure what I'm doing wrong.
Here is my code for my ViewController.m
for (NSObject *photo in photos) {
UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
dView.backgroundColor = [UIColor whiteColor];
dispatch_async(dispatch_get_main_queue(),^{
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
myImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//Get image from url
[self.imageView setImageWithURL:imageURL];
[self.myImage addSubview:imageView];
priceLabel.text = [[[jsonDictionary objectForKey:#"site"] objectAtIndex:index] objectForKey:#"price"];
[imageView addSubview:dView];
});
}
}];
[task resume];
}
you are adding dView as subview for your image view may be it is [dView addSubview:imageView];
try this. the main issue is you are not setting frame for image view
for (NSObject *photo in photos) {
//UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
//dView.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(160,250,258,229)];
[self.myImage addSubview:imageView];
priceLabel.text = [[[jsonDictionary objectForKey:#"site"] objectAtIndex:index] objectForKey:#"price"];
//[imageView addSubview:dView];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"URL"]];
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData) {
[imageView setImage:[UIImage imageWithData:imgData]];
}
});
});
}
Related
I have two arrays, one with usernames and the other with played games, I want to match the profile picture that is in the username array to the yourName in played games however I cannot seem to get it to work.
for(id object in self.playedGames){
PFObject *obb = object;
for(int l = 0; l <self.Userarray1.count;l++){
if([[obb objectForKey:#"yourName"]isEqual:[[self.Userarray1 valueForKey:#"username"]objectAtIndex:l]] ){
PFFile *imageFile = [[self.Userarray1 valueForKey:#"profilePic"]objectAtIndex:l];
if(imageFile !=nil){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSURL* imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
UIImage *newImageset = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
UIImageView *imgVew = [[UIImageView alloc] initWithFrame:CGRectMake(4, 5, 50, 50)];
imgVew.image = newImageset;
imgVew.opaque = YES;
[ImageArray addObject:imgVew];
NSLog(#"imageArray count = %i",ImageArray.count);
if(ImageArray.count == self.playedGames.count){
[self getStuff];
}
});
});
}
else{
UIImage *NoPP = [UIImage imageNamed:#"friends_tab.png"];
NoPP = [self imageScaledToSize:CGSizeMake(50, 50)withImage:NoPP];
UIImageView *NoPPView = [[UIImageView alloc]initWithFrame:CGRectMake(4, 5, 50, 50)];
NoPPView.image = NoPP;
NoPPView.opaque = YES;
[ImageArray addObject:NoPPView];
}
}
}
}
Realized that i was doing asynchronous work and synchronous work so the ImageArray was being added at different times so I fixed it with this solution
if(imageFile !=nil){
UIImageView *imgVew = [[UIImageView alloc]initWithFrame:CGRectMake(4, 5, 50, 50)];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSURL* imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];
NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];
UIImage *newImageset = [UIImage imageWithData:imageData];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
imgVew.image = newImageset;
imgVew.opaque = YES;
});
});
[ImageArray addObject:imgVew];
NSLog(#"imageArray count = %i",ImageArray.count);
if(ImageArray.count == self.playedGames.count){
[self getStuff];
}
}
it also made iterating through that function faster as well
I have the following code to play animation with images:
-(void)playPopAnimation:(void (^)(void))completion{
__block NSMutableArray *images;
float animationDuration = 1.0f;
images = [[NSMutableArray alloc] initWithCapacity:26];
for(int i=1; i<26; i++){
NSString *fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"bulut_pop%d",i] ofType:#"png"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];
//UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:#"bulut_pop%d.png",i]];
[images addObject:image];
}
[Helper playSound:#"button_pop"];
UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.itemImage.frame];
imageView.animationImages = images;
imageView.animationDuration = animationDuration;
imageView.animationRepeatCount = 1; // 0 = nonStop repeat
[self addSubview:imageView];
[imageView startAnimating];
self.imageButton.userInteractionEnabled = NO;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, animationDuration * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
//[self.delegate myItemCell:self cellDidTappedFor:[self item]];
self.imageButton.userInteractionEnabled = YES;
images = nil;
});
Each time I call this function, memory increases by 10 MB. What is the good way to play animated image views?
Each time you call this method, you add a new image view (with its array of images) to your view. You should refactor your code so that the creation of the array and imageView are inside an if clause testing whether imageView is nil, so that part only runs the first time the method is called (and make imageView a property).
I am using GCD to download the header image for the UITableView.
When I use dispatch_async, the image does not show up at all, and when I use dispatch_sync, it still a synchronous download. How do I fix this ?
eventDetailsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
eventDetailsTable.dataSource = self;
eventDetailsTable.delegate = self;
[self.view addSubview:eventDetailsTable];
NSString *headerImageUrl = [NSString stringWithFormat:#"%#%#", [currentEvent objectForKey:#"baseurl"], [currentEvent objectForKey:#"sessionimage"]];
NSURL *headerImageURL = [NSURL URLWithString:headerImageUrl];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:headerImageURL];
UIImage *headerImage = [UIImage imageWithData:imageData];
UIImageView *headerImageView = [[UIImageView alloc] initWithImage:headerImage];
eventDetailsTable.tableHeaderView = headerImageView;
});
When you update UI, you must do it on the main thread. So here is solution:
dispatch_async(global_queue, ^{
//Do your work
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI
});
});
how can i get image from UIPasteboard after the user click copy actually i can get Nsdata from UIPasteboard but i think it needs specific coding to be assigned to uiimageview how can i do this ? thanks for help.
NSData *data = // however you obtain the data
UIImage *image = [[UIImage alloc] initWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
Even better:
UIImage *image = [[UIPasteboard generalPasteboard] image];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
I'm a novice in this, so i would like to know what is missing in my code to download/show these images:
self.carView.contentSize = CGSizeMake(280*self.carImages.count-280, 200);
self.carView.pagingEnabled = YES;
CGRect imageRect = CGRectMake(0.0, 0.0,280*self.carImages.count-280, 200);
UIGraphicsBeginImageContext(imageRect.size);
UIImageView *carImg = [[UIImageView alloc] init];
for (int i = 1; i < self.carImages.count; i++) {
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[self.carImages objectAtIndex:i]]];
UIImage *carImage = [[UIImage alloc] initWithData:imageData];
[carImage drawAtPoint:CGPointMake(280*(i-1), 0)];
[carImage release];
[imageData release];
NSLog(#"%d",i);
}
carImg.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.carView addSubview:carImg];
[carImg release];
carView is my ScrollView.
carImages is a MutableArray with the "URL" of the images.
the pagingEnabled and contentSize are being set, but no images.
I bet i'm missing something very stupid, as i allways do.