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
Related
I am using the below code to load an image from json response and display the image in UICollectionView. But the numberofItems in Section count does return 0 . It means imageArray2.count returns 0. It does not work. Can anybody tell me where I am doing wrong in the below code? imageArray2 is my NSArray.
// JSON RESPONSE
claimImages = (
{
"image_url" = "http://zapponomics.net/claimservice/parcelimage/555506520image0.jpg";
"img_for" = "Front view of parcel";
"pro_number" = Rita;
}
);
images = (
{
"image_url" = "http://zapponomics.net/claimservice/parcelimage/384270647image0.jpg";
"img_for" = "Front view of parcel";
"pro_number" = Rita;
}
);
NSMutableDictionary *imageDict2 = [jsonDict valueForKey:#"claimImages"];
NSLog(#"Image Dictionary :- %#",imageDict2);
imageArray2 = [imageDict2 valueForKey:#"image_url"];
NSLog(#"My Array Image :- %#",imageArray2);
-(void)loadImageFromURL2:(NSURL *) url callback:(void(^)(UIImage *image1))callback
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *imageData1 = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(),^{
UIImage *image1 = [UIImage imageWithData:imageData1];
callback(image1);
});
});
}
if (collectionView.tag == 601)
{
cell = [afterParcelCollectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
UIImageView *myImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 80)];
myImageView2.tag = 102;
[cell.contentView addSubview:myImageView2];
NSString *myImage1= [imageArray2 objectAtIndex:indexPath.row];
[self loadImageFromURL2:[NSURL URLWithString:[myImage1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]] callback:^(UIImage *image1){
myImageView2.image =image1;
}];
}
return cell;
Try the following code and replace your code:
NSArray *claimImagesArray = [jsonDict valueForKey:#"claimImages"];
NSLog(#"%#", claimImagesArray);
NSString *imagePath = [[claimImagesArray objectAtIndex:0] valueForKey:#"image_url"];
NSLog(#"My Image :- %#", imagePath);
I have a Method wich create an UIScrollView and your subviews programmatically. One of your views is a MoviewPlayer...While the scrollview is being created, I need to allow the user to exit the screen (before the loading ends). Then, I've called this method in another NSThread.
But I'm getting the following error:
[__NSCFData _pickableRoutesDidChangeNotification:]: unrecognized selector sent to instance
I think it's not possible to do it in another Thread. Then, How could I allow the user to exit the view before the loading ends?
Theres my code:
-(void) viewDidAppear:(BOOL)animated {
[NSThread detachNewThreadSelector:#selector(criarScrollMidias:) toTarget:self withObject:self.eventoAtual];
}
-(void) criarScrollMidias:(Evento *)evento {
NSLog(#"criar scroll midias");
NSMutableArray *listaImagens = evento.atracao.images;
NSMutableArray *listaVideos = evento.atracao.videos;
if([listaVideos count] > 0) {
self.listaVideoPlayers = [[NSMutableArray alloc]init];
}
[self.scrollImVd setPagingEnabled:YES];
if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
{
UIImageView *imgViewAtracao = [[UIImageView alloc]initWithImage:self.eventoAtual.atracao.imageLarge];
[imgViewAtracao setContentMode:UIViewContentModeScaleAspectFit];
[imgViewAtracao setFrame:CGRectMake(0, 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];
[self.scrollImVd addSubview:imgViewAtracao];
}
for (int iCnt = 0; iCnt < [listaImagens count]; iCnt++) {
NSString *imgURL = [listaImagens objectAtIndex:iCnt];
UIImage *imagemAtracao = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]];
UIImageView *imgViewAtracao = [[UIImageView alloc]initWithImage:imagemAtracao];
[imgViewAtracao setContentMode:UIViewContentModeScaleAspectFit];
if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
[imgViewAtracao setFrame:CGRectMake(0+self.scrollImVd.frame.size.width*(iCnt+1), 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];
else
[imgViewAtracao setFrame:CGRectMake(0+self.scrollImVd.frame.size.width*iCnt, 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];
[self.scrollImVd addSubview:imgViewAtracao];
}
for (int iCnt = 0; iCnt < [listaVideos count]; iCnt++) {
NSString *videoURLStr = [listaVideos objectAtIndex:iCnt];
NSURL *videoURL = [NSURL URLWithString:videoURLStr];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[moviePlayer setShouldAutoplay:NO];
[moviePlayer.view setBackgroundColor:[UIColor colorWithPatternImage:[self gerarPreviewVideo:videoURL]]];
CGFloat posicaoInicial = 0.0;
if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
posicaoInicial = self.scrollImVd.frame.size.width * ([listaImagens count] + 1);
else
posicaoInicial = self.scrollImVd.frame.size.width * [listaImagens count];
[moviePlayer.view setFrame:CGRectMake(posicaoInicial+self.scrollImVd.frame.size.width*iCnt, 0, self.scrollImVd.frame.size.width, self.scrollImVd.frame.size.height)];
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(moviePlayer.view.frame.origin.x+(self.scrollImVd.frame.size.width/2)-34, moviePlayer.view.frame.origin.y+(self.scrollImVd.frame.size.height/2)-33, 67, 66)];
[playButton setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateNormal];
playButton.tag = iCnt;
[playButton addTarget:self action:#selector(playButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollImVd addSubview:moviePlayer.view];
[self.scrollImVd addSubview:playButton];
[self.listaVideoPlayers addObject:moviePlayer];
}
if(![self.eventoAtual.atracao.imageLarge isKindOfClass:[NSNull class]])
[self.scrollImVd setContentSize:CGSizeMake(self.scrollImVd.frame.size.width * ([listaImagens count] + [listaVideos count] + 1), self.scrollImVd.frame.size.height)];
else
[self.scrollImVd setContentSize:CGSizeMake(self.scrollImVd.frame.size.width * ([listaImagens count] + [listaVideos count]), self.scrollImVd.frame.size.height)];
[self.viewLoading setHidden:YES];
}
I've debugged and discovered that the problem is when I try to generate the preview in another Thread:
-(UIImage *) gerarPreviewVideo:(NSURL *)videoURL {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
AVAssetImageGenerator *generateImg = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *error = NULL;
CMTime time = CMTimeMake(1, 65);
CGImageRef refImg = [generateImg copyCGImageAtTime:time actualTime:NULL error:&error];
UIImage *thumbnail= [[UIImage alloc] initWithCGImage:refImg];
return thumbnail;
}
How could I resolve it?
Just updated to iOS 8.0 this morning. I have a piece of code which created some labels and displays them on the scree next to some images. It was working fine before the update. Now the label and the image is not displaying. Can some one please help as I have tried everything I can think of and nothing has solved my issue and couldn't find anyone else with a similar issue. Code attached below.
UIView *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, kCategoryLabelHieght)];
labelView.backgroundColor = [self colorWithHexString:#"2D2D2D"];
UILabel *categoryTitle = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 200, kCategoryLabelHieght)];
categoryTitle.textAlignment = NSTextAlignmentLeft;
categoryTitle.text = text;
categoryTitle.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
codeAppDelegate *appDelegate = (codeAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *fetchedCategory = [appDelegate getCategoryByName:text];
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(10, 2, 20, 20)];
if(fetchedCategory.count)
{
for (int t=0; t<fetchedCategory.count; t++) {
Dealcategory_List *catList = [fetchedCategory objectAtIndex:t];
NSString *titleImage = [self URLEncodeString:[NSString stringWithFormat:#"%#",catList.image]];
NSURL *url = [NSURL URLWithString:titleImage];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *imgTabs = [UIImage imageWithData:data];
// [header2 setImage:imgTabs forState:UIControlStateNormal];
[imgTabs resizableImageWithCapInsets: UIEdgeInsetsMake(5, 10, 2, 0) resizingMode: UIImageResizingModeTile];
img.image= imgTabs;
}
}
else
{
NSArray *fetchedNewsCategory = [appDelegate getNewsCategoryByName:text];
if(fetchedNewsCategory.count > 0)
{
for (int t=0; t<fetchedNewsCategory.count; t++) {
News_Categories *catList = [fetchedNewsCategory objectAtIndex:t];
NSString *titleImage = [self URLEncodeString:[NSString stringWithFormat:#"%#",catList.category_image]];
NSURL *url = [NSURL URLWithString:titleImage];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *imgTabs = [UIImage imageWithData:data];
// [header2 setImage:imgTabs forState:UIControlStateNormal];
[imgTabs resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 2, 0) resizingMode: UIImageResizingModeTile];
img.image= imgTabs;
}
}
else
{
NSArray *fetchedCompetition = [appDelegate getCompetitionByName:text];
if(fetchedCompetition.count > 0)
{
for (int t=0; t<fetchedCompetition.count; t++) {
Competitions_List *catList = [fetchedCompetition objectAtIndex:t];
NSString *titleImage = [self URLEncodeString:[NSString stringWithFormat:#"%#",catList.competition_tab_image]];
NSURL *url = [NSURL URLWithString:titleImage];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *imgTabs = [UIImage imageWithData:data];
// [header2 setImage:imgTabs forState:UIControlStateNormal];
[imgTabs resizableImageWithCapInsets: UIEdgeInsetsMake(10, 10, 2, 0) resizingMode: UIImageResizingModeTile];
img.image= imgTabs;
break;
}
}
}
}
categoryTitle.textColor = color;
[labelView addSubview:img];
[labelView addSubview:categoryTitle];
[self.contentView addSubview:labelView];
UIView *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, kCategoryLabelHieght)];
You are defining a UIView but allocating a UILabel. iOS 8 is less tolerant to errors like this. Try to change it to
UIView *labelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, kCategoryLabelHieght)];
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
});
});
having a problem adding multiple ImageViews to my view. I have an array of images and what to cycle through them and add to my view. This snippet adds an image to view, but with no frame!
- (void)cycleImages:(NSArray *)images {
//create an image view
int fromLeft = 5;
int profilesCount = [images count] - 1;
for(int n = 0; n <= profilesCount; n++){
//add image view to view
[self.view addSubview:[[[UIImageView alloc] initWithFrame:CGRectMake(5, fromLeft, 48, 48)] initWithImage:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:n]]]]]];
fromLeft = fromLeft + 53;
NSLog(#"%d", fromLeft);
}
}
Thanks for your help!
Dex
Its hard to read such code and you have mem leaks. Please, try this.
- (void)cycleImages:(NSArray *)images
{
int fromLeft = 5;
NSURL* url = nil;
UIImage* img = nil;
UIImageView* imgView = nil;
NSData* imgData = nil;
for (NSString* imgURLStr in images)//Using enumeration much more easy and in ObjC style
{
//Put breakpoint here and check all varibles initialized coorecly step by step
url = [NSURL URLWithString:imgURLStr];
imgData = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:imgData];
imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = CGRectMake(5, fromLeft, 48, 48);
[self.view addSubView:imgView];
fromLeft = fromLeft + 53;
NSLog(#"%d", fromLeft);
//Clean up
[img release];
[imgView release];
}
}
Thanks!