ios - captureImage.image;之间的区别和 [captureImage].image;

标签 ios objective-c

我正在尝试使用 captureImage.image;然而,在我的代码中,有人可以解释这段代码的含义以及正确的 captureImage.image;[captureImage].image;[captureImage.image];

我在这段代码中使用它 对于.h

    IBOutlet UIPickerView     *SaveTopicker;
    NSMutableArray            *arraygenre;
}

@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;

@property (weak, nonatomic) IBOutlet UIView *imagePreview;
@property (weak, nonatomic) IBOutlet UIView *saveImage;
@property (weak, nonatomic) IBOutlet UIImageView *captureImage;
@property (weak, nonatomic) IBOutlet UISegmentedControl *cameraSwitch;
@property (weak, nonatomic) IBOutlet UIView *pickerViewContainer;

@property (nonatomic, retain) UIAccelerometer *accelerometer;
@property (weak,nonatomic) IBOutlet UIScrollView *BGScrollView;
- (IBAction)saveButton:(id)sender;
- (IBAction)closeButton:(id)sender;



- (IBAction)switchCamera:(id)sender;
- (IBAction)snapImage:(id)sender;

实现文件

  - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        //fetch Category Name from the array used to fill the Picker View
        NSString *categoryName= [arraygenre objectAtIndex:row];
        NSString *fPath = [documentsDirectory stringByAppendingPathComponent:categoryName];
        NSFileManager *fileManager=[[NSFileManager alloc]init];
        [fileManager createDirectoryAtPath:fPath withIntermediateDirectories:YES attributes:nil error:nil];

        [captureImage.image];
        [data writeToFile:fPath atomically:YES];
        NSData *data= UIImagePNGRepresentation(image);

    }
- (IBAction)snapImage:(id)sender {
    if (!haveImage) {
        captureImage.image = nil; //remove old image from view
        captureImage.hidden = NO; //show the captured image view
        imagePreview.hidden = YES; //hide the live video feed
        [self capImage];
    }
    else {
        captureImage.hidden = YES;
        imagePreview.hidden = NO;
        haveImage = NO;
    }
}

- (void) capImage { //method to capture image from AVCaptureSession video feed
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections) {

        for (AVCaptureInputPort *port in [connection inputPorts]) {

            if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
                videoConnection = connection;
                break;
            }
        }

        if (videoConnection) {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

        if (imageSampleBuffer != NULL) {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
            [self processImage:[UIImage imageWithData:imageData]];
        }
    }];
}


- (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);
        captureImage.hidden = NO;

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

但是,我在第一个 captureImage.image 代码中收到错误消息,指出预期的标识符 并在[data writeToFile:fPath atomically:YES];中使用未声明的标识符数据,并在NSData *data= UIImagePNGRepresentation(image);中使用未声明的标识符图像/p>

我做错了什么吗?

最佳答案

您可能会提供一些更多信息,说明您正在努力实现什么,以及什么是行不通的。回答你的问题:captureImage.image 是唯一一个具有三种有效语法的

编辑:我看到你已经用一些代码更新了你的答案,但我不清楚 captureImage 行应该做什么。 captureImage.image 调用 captureImage 对象上的图像方法(通常是一个属性),但该对象没有声明。

编辑 2: 假设 captureImage 存在于此方法之外,您的最后几行可能应该是这样的:

UIImage *image = captureImage.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:fPath atomically:YES];

关于ios - captureImage.image;之间的区别和 [captureImage].image;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18203791/

相关文章:

ios - 禁用/隐藏一个 View Controller 的 Root View Controller

ios - 将 subview 添加到 uistackview 不适用于按钮单击

ios - 循环获取相关元素的indexPath

ios - 打开网址 :options:completionHandler: some questions

ios - 将字体大小设置为整数?

ios - 对核心数据 : "Select * WHERE "attribute"== "Some string" "执行此操作

objective-c - 如何避免图像重定位使用带有 HTML 内容的 NSSharingService 发送电子邮件

objective-c - 越狱的 iOS 从后台应用程序截图

ios - 旋转图像的碰撞检测

objective-c - 使用 Storyboard在 iPAD 上支持多个方向的最佳方法(无 AutoLayout 或 AutoResizing)