Last active
October 10, 2015 21:18
-
-
Save imrekel/3752047 to your computer and use it in GitHub Desktop.
bme-ios - PictureGuess
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if ([sender isKindOfClass:[UIButton class]]) | |
{ | |
PGResultsViewController* resultsVc = segue.destinationViewController; | |
resultsVc.picture = _baseImage; | |
UIButton* correctButton = (UIButton*)[self.view viewWithTag:_correctAnswerIndex+1]; | |
if (sender == correctButton) | |
resultsVc.caption = [NSString stringWithFormat:@"Talált! Ez egy %@.", correctButton.titleLabel.text.lowercaseString]; | |
else | |
resultsVc.caption = [NSString stringWithFormat:@"Sajnos nem talált! Ez egy %@.", correctButton.titleLabel.text.lowercaseString]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
PGPictureManager* pictureMgr = [PGAppDelegate sharedAppDelegate].pictureManager; | |
// véletlen kép és képcímkék lekérése | |
UIImage* baseImage = nil; | |
NSArray* titles = nil; | |
[pictureMgr getRandomPicture:&baseImage titles:&titles pictureTitleIndex:&_correctAnswerIndex]; | |
_baseImage = baseImage; | |
// gombok szövegének beállítása | |
for (int i=0; i<titles.count; i++) | |
{ | |
UIButton* button = (UIButton*)[self.view viewWithTag:i+1]; | |
[button setTitle: [titles objectAtIndex:i] forState: UIControlStateNormal]; | |
} | |
// kép véletlenszerű kivágása | |
CGSize cropSize = CGSizeMake(300, 200); | |
CGRect cropRect = CGRectMake(arc4random() % (int)(baseImage.size.width-cropSize.width) * baseImage.scale, | |
arc4random() % (int)(baseImage.size.height-cropSize.height) * baseImage.scale, | |
cropSize.width * baseImage.scale, cropSize.height * baseImage.scale); | |
CGImageRef croppedImageRef = CGImageCreateWithImageInRect(baseImage.CGImage, cropRect); | |
UIImage *croppedImage = [UIImage imageWithCGImage:croppedImageRef scale:baseImage.scale orientation:baseImage.imageOrientation]; | |
CGImageRelease(croppedImageRef); | |
self.pictureView.image = croppedImage; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* picture : egy véletlenül kiválasztott kép | |
* titles : véletlenül kiválasztott egyedi képcímek | |
* pictureTitleIndex : a kiválasztott képhez tartozó képcím indexe a titles tömbön belül | |
*/ | |
- (void)getRandomPicture:(__autoreleasing UIImage**)picture titles:(__autoreleasing NSArray**)titles pictureTitleIndex:(int*)pictureTitleIndex | |
{ | |
// a kiválasztott kép indexe | |
int selectedPictureIndex = arc4random() % _pictures.count; | |
// egy tömb a még nem kiválasztott képcímek indexével (egyedi képcím kiválasztás miatt szükséges) | |
NSMutableArray *rangeArray = [[NSMutableArray alloc] initWithCapacity:_pictures.count]; | |
for (int i = 0; i<_pictures.count; i++) | |
[rangeArray addObject:[NSNumber numberWithInt:i]]; | |
[rangeArray removeObjectAtIndex:selectedPictureIndex]; | |
// egyedi véletlen képcímek kiválasztása | |
NSMutableArray* titlesToReturn = [NSMutableArray array]; | |
for (int j = 0; j<kChoices-1; j++) | |
{ | |
int randomIndex = arc4random() % rangeArray.count; | |
[titlesToReturn addObject:[[_pictures objectAtIndex:[[rangeArray objectAtIndex:randomIndex] intValue]] valueForKey:@"title"]]; | |
[rangeArray removeObjectAtIndex:randomIndex]; | |
} | |
*pictureTitleIndex = arc4random() % kChoices; | |
[titlesToReturn insertObject:[[_pictures objectAtIndex:selectedPictureIndex] valueForKey:@"title"] atIndex:*pictureTitleIndex]; | |
*titles = titlesToReturn; | |
*picture = [UIImage imageNamed:[[_pictures objectAtIndex:selectedPictureIndex] valueForKey:@"image"]]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (id)init | |
{ | |
if (self = [super init]) | |
{ | |
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Pictures" ofType:@"plist"]; | |
_pictures = [NSArray arrayWithContentsOfFile:filePath]; | |
} | |
return self; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment