Advertisement
Guest User

fluchtpunkt

a guest
Oct 12th, 2010
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  ImageViewController.h
  2. //
  3. //  Created by Matthias Bauch on 12.10.10.
  4. //  Copyright 2010 Matthias Bauch. All rights reserved.
  5. //
  6.  
  7. #import <UIKit/UIKit.h>
  8.  
  9. #warning this is just a quick hack, you should not use this if you don't understand this. There might be leaks, bugs and a lot of whatever.
  10.  
  11. @interface ImageViewController : UIViewController {
  12.     NSString *imagePath;
  13. }
  14. @property (nonatomic, copy) NSString *imagePath;
  15. - (id)initWithImageDirectory:(NSString*)imgPath;
  16. @end
  17.  
  18.  
  19. //
  20. //  ImageViewController.m
  21. //
  22. //  Created by Matthias Bauch on 12.10.10.
  23. //  Copyright 2010 Matthias Bauch. All rights reserved.
  24. //
  25.  
  26. #import "ImageViewController.h"
  27.  
  28.  
  29. @implementation ImageViewController
  30. @synthesize imagePath;
  31.  
  32. - (id)initWithImageDirectory:(NSString*)imgPath {
  33.     if (self = [super init]) {
  34.         imagePath = [imgPath copy];
  35.     }
  36.     return self;
  37. }
  38.  
  39.  
  40. - (UIView *)viewFullOfImagesAtPath:(NSString *)path withSize:(CGSize)size {
  41.     NSError *error = nil;
  42.     NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
  43.     if (!filenames) {
  44.         NSLog(@"Error accessing files: %@ [%@]", [error localizedDescription], error);
  45.         return nil;
  46.     }
  47.     UIView *aView = [[UIView alloc] init];
  48.     CGFloat xOffset = 0;
  49.     for (NSString *filename in filenames) {
  50.         NSString *fullPath = [path stringByAppendingPathComponent:filename];
  51.         UIImage *image = [[[UIImage alloc] initWithContentsOfFile:fullPath] autorelease];
  52.         if (!image)
  53.             continue;
  54.         CGRect frameRect = CGRectMake(xOffset, 0, size.width, size.height);
  55.         UIImageView *imageView = [[[UIImageView alloc] initWithFrame:frameRect] autorelease];
  56.         [imageView setImage:image];
  57.         imageView.contentMode = UIViewContentModeScaleAspectFit;
  58.         [aView addSubview:imageView];
  59.         xOffset += size.width;
  60.     }
  61.     aView.frame = CGRectMake(0, 0, xOffset, size.height);
  62.     return [aView autorelease];
  63. }
  64.  
  65. - (void)viewDidLoad {
  66.     [super viewDidLoad];
  67.    
  68.     UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
  69.     scrollView.pagingEnabled = YES;
  70.     UIView *contentView = [self viewFullOfImagesAtPath:imagePath withSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)];
  71.     NSLog(@"%f %f %f %f", contentView.frame.origin.x, contentView.frame.origin.y, contentView.frame.size.width, contentView.frame.size.height);
  72.     [scrollView addSubview:contentView];
  73.     scrollView.contentSize = CGSizeMake(CGRectGetWidth(contentView.frame), CGRectGetHeight(contentView.frame));
  74.     [self.view addSubview:scrollView];
  75. }
  76.  
  77. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  78.     // Overriden to allow any orientation.
  79.     return YES;
  80. }
  81.  
  82.  
  83. - (void)didReceiveMemoryWarning {
  84.     // Releases the view if it doesn't have a superview.
  85.     [super didReceiveMemoryWarning];
  86.    
  87.     // Release any cached data, images, etc that aren't in use.
  88. }
  89.  
  90.  
  91. - (void)viewDidUnload {
  92.     [super viewDidUnload];
  93.     // Release any retained subviews of the main view.
  94.     // e.g. self.myOutlet = nil;
  95. }
  96.  
  97.  
  98. - (void)dealloc {
  99.     [imagePath release];
  100.     [super dealloc];
  101. }
  102.  
  103.  
  104. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement