2011-04-29 72 views
0

我是一個新的iPhone應用程序開發人員,我需要創建一個應用程序,如照片gallery.Now我的問題是我donno如何將圖像保存到相冊。Three20保存圖像?(關閉)

我建立我的項目是沒有錯誤沒有wiening me.in我的模擬器可以運行該項目。

但我看到任何我添加的東西。希望可以幫助我。謝謝。

我的代碼在這裏。

Three20PhotoDemoAppDelegate.h

#import <UIKit/UIKit.h> 

@interface Three20PhotoDemoAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 

@end 

Three20PhotoDemoAppDelegate.m

#import "Three20PhotoDemoAppDelegate.h" 
#import "AlbumController.h" 
#import <Three20/Three20.h> 

@implementation Three20PhotoDemoAppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Override point for customization after application launch 
    TTNavigator* navigator = [TTNavigator navigator]; 
    TTURLMap* map = navigator.URLMap; 
    [map from:@"demo://album" toViewController: [AlbumController class]]; 

    [navigator openURLAction:[TTURLAction actionWithURLPath:@"demo://album"]]; 
    return YES; 
} 

- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)URL { 
    [[TTNavigator navigator] openURLAction:[TTURLAction actionWithURLPath:URL.absoluteString]]; 
    return YES; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 
@end 

PhotoSource.h

#import <Three20/Three20.h> 
#import <Three20/Three20+Additions.h> 
typedef enum { 
    PhotoSourceNormal = 0, 
    PhotoSourceDelayed = 1, 
    PhotoSourceVariableCount = 2, 
    PhotoSourceLoadError = 4, 
} PhotoSourceType; 

@interface PhotoSource : TTURLRequestModel <TTPhotoSource> { 
    PhotoSourceType _type; 
    NSString* _title; 
    NSMutableArray* _photos; 
    NSArray* _tempPhotos; 
    NSTimer* _fakeLoadTimer; 
} 

- (id)initWithType:(PhotoSourceType)type title:(NSString*)title 
      photos:(NSArray*)photos photos2:(NSArray*)photos2; 

@end 

PhotoSource.m

進口 「PhotoSource.h」

@implementation PhotoSource 
@synthesize title = _title; 

- (void)fakeLoadReady { 
    _fakeLoadTimer = nil; 

    if (_type & PhotoSourceLoadError) { 
     [_delegates makeObjectsPerformSelector: @selector(model:didFailLoadWithError:) 
            withObject: self 
            withObject: nil]; 
    } else { 
     NSMutableArray* newPhotos = [NSMutableArray array]; 

     for (int i = 0; i < _photos.count; ++i) { 
      id<TTPhoto> photo = [_photos objectAtIndex:i]; 
      if ((NSNull*)photo != [NSNull null]) { 
       [newPhotos addObject:photo]; 
      } 
     } 

     [newPhotos addObjectsFromArray:_tempPhotos]; 
     TT_RELEASE_SAFELY(_tempPhotos); 

     [_photos release]; 
     _photos = [newPhotos retain]; 

     for (int i = 0; i < _photos.count; ++i) { 
      id<TTPhoto> photo = [_photos objectAtIndex:i]; 
      if ((NSNull*)photo != [NSNull null]) { 
       photo.photoSource = self; 
       photo.index = i; 
      } 
     } 

     [_delegates makeObjectsPerformSelector:@selector(modelDidFinishLoad:) withObject:self]; 
    } 
} 

- (id)initWithType:(PhotoSourceType)type title:(NSString*)title photos:(NSArray*)photos 
      photos2:(NSArray*)photos2 { 
    if (self = [super init]) { 
     _type = type; 
     _title = [title copy]; 
     _photos = photos2 ? [photos mutableCopy] : [[NSMutableArray alloc] init]; 
     _tempPhotos = photos2 ? [photos2 retain] : [photos retain]; 
     _fakeLoadTimer = nil; 

     for (int i = 0; i < _photos.count; ++i) { 
      id<TTPhoto> photo = [_photos objectAtIndex:i]; 
      if ((NSNull*)photo != [NSNull null]) { 
       photo.photoSource = self; 
       photo.index = i; 
      } 
     } 

     if (!(_type & PhotoSourceDelayed || photos2)) { 
      [self performSelector:@selector(fakeLoadReady)]; 
     } 
    } 
    return self; 
} 

- (id)init { 
    return [self initWithType:PhotoSourceNormal title:nil photos:nil photos2:nil]; 
} 

- (void)dealloc { 
    [_fakeLoadTimer invalidate]; 
    TT_RELEASE_SAFELY(_photos); 
    TT_RELEASE_SAFELY(_tempPhotos); 
    TT_RELEASE_SAFELY(_title); 
    [super dealloc]; 




} 





- (BOOL)isLoading { 
    return !!_fakeLoadTimer; 
} 

- (BOOL)isLoaded { 
    return !!_photos; 
} 

- (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more { 
    if (cachePolicy & TTURLRequestCachePolicyNetwork) { 
     [_delegates makeObjectsPerformSelector:@selector(modelDidStartLoad:) withObject:self]; 

     TT_RELEASE_SAFELY(_photos); 
     _fakeLoadTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self 
                 selector:@selector(fakeLoadReady) userInfo:nil repeats:NO]; 
    } 
} 

- (void)cancel { 
    [_fakeLoadTimer invalidate]; 
    _fakeLoadTimer = nil; 
} 

- (NSInteger)numberOfPhotos { 
    if (_tempPhotos) { 
     return _photos.count + (_type & PhotoSourceVariableCount ? 0 : _tempPhotos.count); 
    } else { 
     return _photos.count; 
    } 
} 

- (NSInteger)maxPhotoIndex { 
    return _photos.count-1; 
} 

- (id<TTPhoto>)photoAtIndex:(NSInteger)photoIndex { 
    if (photoIndex < _photos.count) { 
     id photo = [_photos objectAtIndex:photoIndex]; 
     if (photo == [NSNull null]) { 
      return nil; 
     } else { 
      return photo; 
     } 
    } else { 
     return nil; 
    } 
} 


@end 

Photo.h

#import <Three20/Three20.h> 

@interface Photo : NSObject <TTPhoto> { 
    id<TTPhotoSource> _photoSource; 
    NSString* _thumbURL; 
    NSString* _smallURL; 
    NSString* _URL; 
    CGSize _size; 
    NSInteger _index; 
    NSString* _caption; 
} 

- (id)initWithURL:(NSString*)URL smallURL:(NSString*)smallURL size:(CGSize)size; 

- (id)initWithURL:(NSString*)URL smallURL:(NSString*)smallURL size:(CGSize)size 
      caption:(NSString*)caption; 

@end 

Photo.m

#import "Photo.h" 
@implementation Photo 
@synthesize photoSource = _photoSource, size = _size, index = _index, caption = _caption; 

- (id)initWithURL:(NSString*)URL smallURL:(NSString*)smallURL size:(CGSize)size { 
    return [self initWithURL:URL smallURL:smallURL size:size caption:nil]; 
} 

- (id)initWithURL:(NSString*)URL smallURL:(NSString*)smallURL size:(CGSize)size 
      caption:(NSString*)caption { 
    if (self = [super init]) { 
     _photoSource = nil; 
     _URL = [URL copy]; 
     _smallURL = [smallURL copy]; 
     _thumbURL = [smallURL copy]; 
     _size = size; 
     _caption; 
     _index = NSIntegerMax; 
    } 
    return self; 
} 

- (void)dealloc { 
    TT_RELEASE_SAFELY(_URL); 
    TT_RELEASE_SAFELY(_smallURL); 
    TT_RELEASE_SAFELY(_thumbURL); 
    TT_RELEASE_SAFELY(_caption); 
    [super dealloc]; 


} 
- (void)viewDidLoad { 


} 


- (NSString*)URLForVersion:(TTPhotoVersion)version { 
    if (version == TTPhotoVersionLarge) { 
     return _URL; 
    } else if (version == TTPhotoVersionMedium) { 
     return _URL; 
    } else if (version == TTPhotoVersionSmall) { 
     return _smallURL; 
    } else if (version == TTPhotoVersionThumbnail) { 
     return _thumbURL; 
    } else { 
     return nil; 
    } 
} 

@end 

AlbumController.h

#import <Three20/Three20.h> 

@interface AlbumController : TTPhotoViewController <UIActionSheetDelegate>{ 
    NSArray *images; 
    UIBarButtonItem *_clickActionItem; 
    UIToolbar *_toolbar; 
} 
@property (nonatomic, retain) NSArray *images; 
@property (nonatomic, retain) UIBarButtonItem *_clickActionItem; 
@property (nonatomic, retain) UIToolbar *_toolbar; 
@end 

AlbumController.m

#import "AlbumController.h" 
#import "PhotoSource.h" 
#import "Photo.h" 
@implementation AlbumController 
@synthesize images; 
- (void)loadView { 
    CGRect screenFrame = [UIScreen mainScreen].bounds; 
    self.view = [[[UIView alloc] initWithFrame:screenFrame] 
       autorelease]; 
    CGRect innerFrame = CGRectMake(0, 0, 
            screenFrame.size.width, 
            screenFrame.size.height); 
    _innerView = [[UIView alloc] initWithFrame:innerFrame]; 
    _innerView.autoresizingMask = UIViewAutoresizingFlexibleWidth| 
    UIViewAutoresizingFlexibleHeight; 
    [self.view addSubview:_innerView]; 
    _scrollView = [[TTScrollView alloc] initWithFrame:screenFrame]; 
    _scrollView.delegate = self; 
    _scrollView.dataSource = self; 
    _scrollView.backgroundColor = [UIColor blackColor]; 
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth| 
    UIViewAutoresizingFlexibleHeight; 
    [_innerView addSubview:_scrollView]; 
    UIBarButtonItem *_actionButton = [[UIBarButtonItem alloc] initWithImage: 
        TTIMAGE(@"bundle://Three20.bundle/images/imageAction.png") 
                style:UIBarButtonItemStylePlain target:self action:@selector 
        (popupActionSheet)]; 
    _nextButton = [[UIBarButtonItem alloc] initWithImage: 
        TTIMAGE(@"bundle://Three20.bundle/images/nextIcon.png") 
                style:UIBarButtonItemStylePlain target:self action:@selector 
        (nextAction)]; 
    _previousButton = [[UIBarButtonItem alloc] initWithImage: 
         TTIMAGE(@"bundle://Three20.bundle/images/previousIcon.png") 
                 style:UIBarButtonItemStylePlain target:self action:@selector 
         (previousAction)]; 
    UIBarButtonItem* playButton = [[[UIBarButtonItem alloc] 
            initWithBarButtonSystemItem: 
            UIBarButtonSystemItemPlay target:self action:@selector 
            (playAction)] autorelease]; 
    playButton.tag = 1; 
    UIBarItem* space = [[[UIBarButtonItem alloc] 
         initWithBarButtonSystemItem: 
         UIBarButtonSystemItemFlexibleSpace target:nil action:nil] 
         autorelease]; 
    _toolbar = [[UIToolbar alloc] initWithFrame: 
       CGRectMake(0, screenFrame.size.height - TT_ROW_HEIGHT, 
          screenFrame.size.width, TT_ROW_HEIGHT)]; 
    _toolbar.barStyle = self.navigationBarStyle; 
    _toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth| 
    UIViewAutoresizingFlexibleTopMargin; 
    _toolbar.items = [NSArray arrayWithObjects: 
         _actionButton, space, _previousButton, space, 
         _nextButton, space, nil]; 
    [_innerView addSubview:_toolbar]; 
} 

//(Just to add an action sheet) 
//At the bottom of that codefile -- I added: 
-(void)popupActionSheet { 
    UIActionSheet *popupQuery = [[UIActionSheet alloc] 
           initWithTitle:nil 
           delegate:self 
           cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:nil 
           otherButtonTitles:@"Save Image",nil]; 
    popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    [popupQuery showInView:self.view]; 
    [popupQuery release]; 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: 
(int)buttonIndex { 
    //UIImage* image = [[TTURLCache sharedCache] imageForURL:imageURL]; 
    if (buttonIndex==0){ 
     UIImage* thisImage = [[TTURLCache sharedCache] imageForURL: 
           [_centerPhoto URLForVersion:TTPhotoVersionLarge]]; 
     UIImageWriteToSavedPhotosAlbum(thisImage, nil, nil, nil); 
     //{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL" 
    //message:[_centerPhoto URLForVersion:TTPhotoVersionLarge] delegate:self cancelButtonTitle:@"No" ,otherButtonTitles:@"Yes", nil]; 
     //[alert show];} 
    } 
} 



-(void)createPhotos { 
    images = [[NSArray alloc] initWithObjects: 
       [[[Photo alloc] initWithURL:@"bundle://14.png" smallURL:@"bundle://14.png" 
            size:CGSizeMake(320, 212)] autorelease], 
       [[[Photo alloc] initWithURL:@"bundle://30.png" smallURL:@"bundle://30.png" 
            size:CGSizeMake(320, 212)] autorelease], 
       [[[Photo alloc] initWithURL:@"bundle://back.jpeg" smallURL:@"bundle://back.jpeg" 
            size:CGSizeMake(319, 317)] autorelease], 

       nil]; 

} 



- (void)viewDidLoad { 
    //[self loadView]; 
    [self createPhotos]; // method to set up the photos array 
     self.photoSource = [[PhotoSource alloc] 
         initWithType:PhotoSourceNormal 
         title:@"SexyGirl" 
         photos:images 
         photos2:nil 
         ]; 





} 



@end 
+0

你有沒有錯誤?你在哪一部分? – Aravindhan 2011-04-29 05:27:41

+0

嗨,我編輯ady。 – ahyong87 2011-04-29 06:25:04

回答

0

如果你需要的是保存到相冊上的圖片,你可以使用:

UIImage *myImage; 
UIImageWriteToSavedPhotosAlbum(myImage,nil,nil,nil); 

如果這不是你的問題,然後我不明白這一點。如果你不告訴我們問題在哪裏,發佈三頁代碼是非常沒用的。

+0

嗨,我想要這樣做.http://sol3.typepad.com/tagalong_developer_journa/2009/12/adding-savetoalbum-to-the-ttphotoviewcontroller.html – ahyong87 2011-04-29 07:51:55

+0

感謝你,最後我找到了問題。 – ahyong87 2011-04-29 09:53:25