具有不同名称的NSMutableArray和SetImage的相同算法的工作方式不同

时间:2014-05-23 17:28:49

标签: objective-c arrays nsmutablearray mutable

所以我有一个问题。

我有:

NSMutableArray *photos_imagesArray;
NSMutableArray *myPhotos_imagesArray;

@interface EXViewController内的.h文件中声明。

我的工作是:

对于一个视图 - photos_viewController - 我从photos_imagesArray获取图像数据,并在此视图的UIImageView上使用setImage。一切正常。

现在,当我使用myPhotos_viewControllermyPhotos_imagesArray执行完全相同的操作时,setImage不会启动,myPhotos_imagesArray会被清空。

为什么这样工作呢?我有两个非常相同的算法在两个相同的视图中工作,但我得到不同的结果。所有不同的是几个名字。 XCode甚至没有向我显示任何错误。

更新1(共享代码):

EXViewController.h中我声明了可变数组的地方。

@interface EXViewController : UIViewController <UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIImagePickerController *upload_imagePickerCamera;
    UIImagePickerController *upload_imagePickerLibrary;
    UIImage *upload_imageUploaded;
    IBOutlet UIImageView *upload_imageViewUploadedImage;

    // Photos Currently viewed
    NSInteger _photoIndex;
    NSMutableArray *photos_imagesArray;
    NSMutableArray *myPhotos_imagesArray;
}

功能:

对于有效的部分 -

// PHOTOS

-(void)goToPhotos {

    [photos_imagesArray removeAllObjects];

    _photoIndex = 0;

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *viewControllerPhotos = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewControllerPhotos"];
    [self  presentViewController:viewControllerPhotos animated:true completion:nil];

    PFQuery *query_photos = [PFQuery queryWithClassName:@"Photo"];
    [query_photos orderByDescending:@"createdAt"];
    [query_photos findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // Copy objects array to photos array
            photos_imagesArray = [objects mutableCopy];

            // Set Current photo
            [self photos_setCurrentPhoto];


        } else {

            NSString *errorString = [error userInfo][@"error"];
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];

        }
    }];
}

-(void)photos_setCurrentPhoto{

    if ([photos_imagesArray count] != 0) {

    PFFile *userImageFile = photos_imagesArray[_photoIndex][@"imageFile"];
    [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
            UIImage *image = [UIImage imageWithData:imageData];
            [self.photos_imageViewCurrent setImage:image];
        }
    }];

    }
}

对于那些没有 -

的部分
// MY PHOTOS

-(void)myPhotos_goToMyPhotos{

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *viewControllerPhotos = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewControllerMyPhotos"];
    [self  presentViewController:viewControllerPhotos animated:true completion:^{

        PFQuery *query_photos = [PFQuery queryWithClassName:@"Photo"];

        [query_photos whereKey:@"userId" equalTo:[PFUser currentUser].objectId];
        [query_photos orderByDescending:@"createdAt"];

        [query_photos findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                _photoIndex = 0;

                // Copy objects array to photos array
                myPhotos_imagesArray = [objects mutableCopy];

                NSLog(@"%i",[myPhotos_imagesArray count]);

                // Set Current photo
                [self myPhotos_setCurrentPhoto];


            } else {

                NSString *errorString = [error userInfo][@"error"];
                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];

            }
        }];

    }];

}

-(void)myPhotos_setCurrentPhoto{

    if ([myPhotos_imagesArray count] != 0) {

        // Set photo in image view
        PFFile *userImageFile = myPhotos_imagesArray[_photoIndex][@"imageFile"];
        [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
            if (!error) {
                UIImage *image = [UIImage imageWithData:imageData];
                self.myPhotos_imageViewCurrentPhoto.image = image;
            }
        }];

    }
}

P.S我在完成回调内外尝试了算法 - 结果相同。

更新2(添加了整个EXViewController.h文件):

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface EXViewController : UIViewController <UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIImagePickerController *upload_imagePickerCamera;
    UIImagePickerController *upload_imagePickerLibrary;
    UIImage *upload_imageUploaded;
    IBOutlet UIImageView *upload_imageViewUploadedImage;

    // Photo Currently viewed
    NSInteger _photoIndex;
    NSMutableArray *photos_imagesArray;
    NSMutableArray *myPhotos_imagesArray;
}

// Login Form
@property (strong, nonatomic) IBOutlet UITextField *login_fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *login_fieldPassword;
- (IBAction)login_buttonSignInAction:(id)sender;
- (IBAction)login_buttonForgotAction:(id)sender;


// Sign Up Form
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldPassword;
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldCountry;
@property (strong, nonatomic) IBOutlet UISegmentedControl *signup_fieldSex;
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldYear;
- (IBAction)buttonSignupAction:(id)sender;

// Photos
@property (weak, nonatomic) IBOutlet UIImageView *photos_imageViewCurrent;
- (IBAction)photos_buttonRatePlus:(id)sender;
- (IBAction)photos_buttonRateMinus:(id)sender;


// Menu
- (IBAction)menu_buttonSignOutAction:(id)sender;
- (IBAction)menu_buttonMyPhotos:(id)sender;


// Upload Photo
- (IBAction)upload_buttonTakePhoto:(id)sender;
- (IBAction)upload_buttonPhotoAlbum:(id)sender;
- (IBAction)upload_buttonUpload:(id)sender;
@property (strong, nonatomic) IBOutlet UIProgressView *upload_progressViewUpload;
@property (strong, nonatomic) IBOutlet UILabel *upload_labelImageUploaded;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *upload_gravity;

// My Photos
@property (weak, nonatomic) IBOutlet UIImageView *myPhotos_imageViewCurrentPhoto;
@property (weak, nonatomic) IBOutlet UILabel *myPhotos_labelRating;
@property (weak, nonatomic) IBOutlet UILabel *myPhotos_labelVotes;
- (IBAction)myPhotos_buttonNext:(id)sender;
- (IBAction)myPhotos_buttonPrevious:(id)sender;



@end

0 个答案:

没有答案