-9

我声明了代码

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

但是我收到一个错误,它指向 = 标记,说 Expected ';' 在声明列表错误的末尾,我认为这不会发生在间距问题上。怎么了?

- (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];

    }
    }



}
4

1 回答 1

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
于 2013-08-16T06:09:08.080 回答