Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for retina view tests #103

Merged
merged 2 commits into from

2 participants

@x2on
Owner

Support for retina view test
fixes #102

@johnboiles

This is an iOS6-only API. Please only use APIs available in iOS versions >= 4.3. In this case perhaps use:

imageWithCGImage:scale:orientation:

@johnboiles

It's confusing to have a variable named filename and a different one named fileName. You could avoid this by naming the variable something different, or doing the operation Inline. You also don't need lastPathComponent since filename is expected to be only the name of the file, not the path.

filePath = [[NSBundle mainBundle] pathForResource:[filename stringByDeletingPathExtension] ofType:[filename pathExtension]];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
image = [UIImage imageWithData:data scale:[UIScreen mainScreen].scale];
@johnboiles

iOS6 only API

@johnboiles
Collaborator

Thanks for the pull request! Make the fixes and we'll pull this in.

@x2on
Owner

Restored the compatibility - but used the iOS6 implementation if available (it's faster).

@johnboiles

convention is to put the else and brackets on the same line

} else {
@johnboiles

might be worth adding a comment noting that this API is iOS6 only

@johnboiles johnboiles merged commit ca5a8ec into gh-unit:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 25, 2012
  1. @x2on

    Support for retina view tests

    x2on authored
  2. @x2on

    Restore iOS 4.3-5.0 compatibility

    x2on authored
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 3 deletions.
  1. +20 −3 Classes/GHViewTestCase.m
View
23 Classes/GHViewTestCase.m
@@ -85,11 +85,13 @@ + (UIImage *)readSavedTestImageWithFilename:(NSString *)filename {
NSString *filePath = [self approvedTestImagePathForFilename:filename];
GHUDebug(@"Trying to load image at path %@", filePath);
// First look in the documents directory for the image
- UIImage *image = [UIImage imageWithContentsOfFile:filePath];
+ UIImage *image = [GHViewTestCase _imageFromFilePath:filePath];
// Otherwise look in the app bundle
if (image) GHUDebug(@"Found image in documents directory");
if (!image) {
- image = [UIImage imageNamed:filename];
+ NSString* extension = [filename pathExtension];
+ filePath = [[NSBundle mainBundle] pathForResource:[filename stringByDeletingPathExtension] ofType:extension];
+ image = [GHViewTestCase _imageFromFilePath:filePath];
if (image) GHUDebug(@"Found image in app bundle");
}
return image;
@@ -115,7 +117,7 @@ + (void)clearTestImages {
+ (UIImage *)imageWithView:(UIView *)view {
[view setNeedsDisplay];
- UIGraphicsBeginImageContext(view.frame.size);
+ UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
CALayer *layer = view.layer;
CGContextRef context = UIGraphicsGetCurrentContext();
[layer renderInContext:context];
@@ -223,6 +225,21 @@ - (void)_setUp {
imageVerifyCount_ = 0;
}
++ (UIImage *)_imageFromFilePath:(NSString *)filePath {
+ UIImage *image;
+
+ NSData *imageData = [NSData dataWithContentsOfFile:filePath];
+ CGFloat scale = [UIScreen mainScreen].scale;
+ if ([UIImage respondsToSelector:@selector(imageWithData:scale:)]) {
+ image = [UIImage imageWithData:imageData scale:scale];
+ }
+ else {
+ UIImage *imageWithoutScale = [UIImage imageWithData:imageData];
+ image = [UIImage imageWithCGImage:imageWithoutScale.CGImage scale:scale orientation:UIImageOrientationUp];
+ }
+ return image;
+}
+
#pragma mark Public
- (CGSize)sizeForView:(UIView *)view {
Something went wrong with that request. Please try again.