预期';'在声明结束列表错误

时间:2013-08-16 05:30:33

标签: ios objective-c nsmutablearray

我宣布了代码

NSMutableArray *allImagesArray = [[NSMutableArray alloc]init];

然而,我得到一个指向=标记的错误 预期';'在声明结束列表错误,我不认为这是出现间距问题。发生了什么事?

- (void) processImage:(UIImage *)image { //process captured image, crop, resize and rotate
    haveImage = YES;

    if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) { //Device is ipad
        // Resize image
        UIGraphicsBeginImageContext(CGSizeMake(768, 1022));
        [image drawInRect: CGRectMake(0, 0, 768, 1022)];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGRect cropRect = CGRectMake(0, 130, 768, 768);
        CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);
        //or use the UIImage wherever you like

        [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

        CGImageRelease(imageRef);

    }else{ //Device is iphone
        // Resize image
        UIGraphicsBeginImageContext(CGSizeMake(320, 426));
        [image drawInRect: CGRectMake(0, 0, 320, 426)];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGRect cropRect = CGRectMake(0, 55, 320, 320);
        CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);

        [captureImage setImage:[UIImage imageWithCGImage:imageRef]];

        CGImageRelease(imageRef);
    }

    //adjust image orientation based on device orientation
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) {
        NSLog(@"landscape left image");

        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
        [UIView commitAnimations];

    }
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
        NSLog(@"landscape right");

        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(90));
        [UIView commitAnimations];

    }
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"upside down");
        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(180));
        [UIView commitAnimations];

    }
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
        NSLog(@"upside upright");
        [UIView beginAnimations:@"rotate" context:nil];
        [UIView setAnimationDuration:0.5];
        captureImage.transform = CGAffineTransformMakeRotation(DegreesToRadians(0));
        [UIView commitAnimations];

        NSArray *directoryNames = [NSArray arrayWithObjects:@"hats",@"bottoms",@"right",@"left",nil];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

        for (int i = 0; i < [directoryNames count] ; i++) {
            NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
            if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
                [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder

        NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; // "right" is at index 2, per comments & code
        NSString *filePath = [filePath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; // you maybe want to incorporate a timestamp into the name to avoid duplicates
        NSData *imageData = UIImagePNGRepresentation(captureImage.image);
        [imageData writeToFile:filePath atomically:YES];

    }
    }



}

1 个答案:

答案 0 :(得分:0)

为了清楚起见,我的意思是你的头文件(.h文件)应如下所示:

// --------------------------------------------------------
// This is your class header file (it has a .h extension)
// --------------------------------------------------------
#import <UIKit/UIKit.h>

@interface MyClass : UIViewController
{
    // ----------------------------------------------
    // Declare your array here in the header file
    // ----------------------------------------------
    NSMutableArray *allImagesArray;
}

@end

然后在您的实现文件(.m文件)

// --------------------------------------------------------
// This is your implementation file (it has a .m extension)
// --------------------------------------------------------

@implementation MyClass

-(id)init
{
    self = [super init];

    if(self)
    {
       // -----------------------------------------------
       // Initialise your array here
       // -----------------------------------------------

       allImagesArray = [[NSMutableArray alloc] init];
    }

    return self;
}

// the rest of your code 

@end

更新

是的,您也可以在viewDidLoad()中初始化您的数组。这可能就是你应该去做的地方:D

我所说的是不写这个:

NSMutableArray *allImagesArray = [[NSMutableArray alloc ]allImagesArray = [[NSMutableArray alloc] init];

这是无效的语法。

声明和初始化的语法如下:

NSMutableArray *allImagesArray = [[NSMutableArray alloc] init];

但你不能把它放在头文件中。所以你不应该这样做:

// --------------------------------------------------------
// This is your class header file (it has a .h extension)
// --------------------------------------------------------
#import <UIKit/UIKit.h>

@interface MyClass : UIViewController
{
    // ----------------------------------------------
    // Don't write it like this
    // ----------------------------------------------
    NSMutableArray *allImagesArray = [[NSMutableArray alloc] init];
}

@end