2012-04-11 52 views
-1

我正在使用AGImagePickerController。我很難搞清楚如何將選中的圖像導入我的iCarousel這是另一個傳送帶。我知道其中的success block包含選定的圖像。我似乎無法將其導入我的awakeFromNib或將其放入數組中。確定AGImagePickerController中的選定照片

這裏是我的代碼調用AGImagePickerController:

-(IBAction) cameraRoll {AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) { 

     if (error == nil) 
     { 
      NSLog(@"User has cancelled."); 
      [self dismissModalViewControllerAnimated:YES]; 
     } else 
     {  
      NSLog(@"Error: %@", error); 

      // Wait for the view controller to show first and hide it after that 
      double delayInSeconds = 0.5; 
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
       [self dismissModalViewControllerAnimated:YES]; 
      }); 
     } 

     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES]; 

    } andSuccessBlock:^(NSArray *info) { 
     NSLog(@"Info: %@", info); 
     [self dismissModalViewControllerAnimated:YES]; 

     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES]; 
    }]; 

    [self presentModalViewController:imagePickerController animated:YES]; 
    [imagePickerController release]; 
} 

在我awakeFromNib:

- (void)awakeFromNib 
{  
    if (self) { 

     self.images = [NSMutableArray arrayWithObjects:@"111.jpg", 
         @"112.jpg", 
         @"113.jpg", 
         @"114.jpg", 
         @"115.jpg", 
         @"116.jpg", 
         @"117.jpg", 
         @"118.png", 
         @"119.jpg", 
         @"120.jpg", 
         nil]; 

    } 
} 

然後我實現這個我的旋轉木馬:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index 
{ 
    //create a numbered view 
    UIView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[images objectAtIndex:index]]]; 
    return view; 
} 

回答

5

你應該能夠使用您的NSLog語句查看info array的內容。看到該日誌語句的內容會很有幫助。

,我發現這個here和它可能工作:

for (i = 0; i < info.count; i++) { 
    ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation]; 
    UIImage * image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; 
} 

編輯

您需要保存的圖像(在內存或將其寫入文件)的用戶選擇它們後。如果您想將其寫入文件,你能做到這一點,像這樣:

[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; 

如果AGImagePickerController代碼可在您iCarousel類,你可以將圖像只需添加到您的images陣列。你successBlock會是這個樣子:

self.images = [NSMutableArray new]; 

for (i = 0; i < info.count; i++) { 
    ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation]; 
    UIImage * image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; 
    [self.images addObject:image]; 
} 

最後,在你的iCarousel代碼,你需要從您的圖像陣列或從磁盤讀取圖像(取決於您選擇保存)。如果您在內存中保存它們 你的代碼看起來是這樣的:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index 
{ 
    return [[UIImageView alloc] initWithImage:[self.images objectAtIndex:index]]; 
} 

或者,如果你決定將圖像保存到磁盤,那麼你可以重新使用[UIImage imageWithContentsOfFile:filePath];的UIImage的對象。

+0

我會把它放入我的iCarousel的awakeFromNib嗎? – Bazinga 2012-05-26 16:33:45

+0

此代碼會進入您提供的代碼的successBlock。沒有看到你的iCarousel代碼,我無法幫助你。 – 2012-05-26 17:20:29

+0

我編輯了我的答案。希望你能再次檢查。 – Bazinga 2012-05-27 14:36:20

相關問題