UIImagePickerController强制其他视图中的图像为完整大小

时间:2013-12-10 12:50:43

标签: ios objective-c uiimagepickercontroller

在我的应用程序的一个位置,我需要使用相机,所以我打电话给UIImagePickerController。不幸的是,一旦我从控制器返回,应用程序中的大多数图片都是全尺寸的,无论他们的UIImageView属性说什么。 UIImageView中的例外似乎是UITableViewCell。这适用于应用程序中的所有视图,而不仅仅是与调用UIImagePickerController的视图控制器直接连接的视图。有一次,当我在弄乱,试图排除故障时,问题似乎自己消失了,虽然我无法复制它。

代码如下。

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                           message:@"Device has no camera"
                                                           delegate:nil
                                                           cancelButtonTitle:@"OK"
                                                           otherButtonTitles:nil];
        [errorAlertView show];
        [_app.navController popPage];
    }
    else if(firstTime){
        UIImagePickerController *picker = [[UIImagePickerController  alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;

        firstTime = false;

        [self presentViewController:picker animated:YES completion:NULL];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *cameraImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];

    NSString *folderName = @"redApp";
    if([_page hasChild:[RWPAGE FOLDER]]){
        folderName = [_page getStringFromNode:[RWPAGE FOLDER]];
    }

    NSDate *datetimeNow = [[NSDate alloc] init];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd_HH-mm-ss-SSS"];
    NSString *filename = [NSString stringWithFormat:@"%@.png",[dateFormatter stringFromDate:datetimeNow]];

    NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *folderPath = [applicationDocumentsDir stringByAppendingPathComponent:folderName];
    NSString *filePath = [folderPath stringByAppendingPathComponent:filename];

    NSError *error = nil;
    if(![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:nil]){
        [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:NO
                                                        attributes:nil error:&error];
    }

    if(error != nil){
        NSLog(@"Create directory error: %@", error);
    }

    [UIImagePNGRepresentation(cameraImage) writeToFile:filePath options:NSDataWritingAtomic error:&error];

    if(error != nil){
        NSLog(@"Error in saving image to disk. Error : %@", error);
    }

    RWXmlNode *nextPage = [_xml getPage:[_page getStringFromNode:[RWPAGE CHILD]]];
    nextPage = [nextPage deepClone];
    [nextPage addNodeWithName:[RWPAGE FILEPATH] value:filePath];

    [_app.navController pushViewWithPage:nextPage];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];

    [_app.navController popPage];
}

修改 扩展上述内容。

应用程序的基础是自定义容器视图控制器,主要用作导航控制器。当用户导航到页面(我称之为视图和视图控制器的组合)时,它将显示在“自定义容器”视图中,而上一页存储在堆栈中。

我的一个页面调用了UIImagePicker。一旦图像选择器再次关闭,并且我返回到应用程序,当我打开新页面时,应用程序中会出现问题。我没有在每个页面上看到问题,但它们在几个独立的页面上。大多数页面看起来完全不受影响,而问题页面似乎不遵守其约束条件。

2 个答案:

答案 0 :(得分:3)

如果你想压缩图像使用

+(UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize
{
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);

// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

CGImageRelease(newImageRef);
UIGraphicsEndImageContext();

return newImage;
}

或         +(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize {

CGFloat scaleFactor = 1.0;


if (image.size.width > targetSize.width || image.size.height > targetSize.height)
    if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or
        scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.

UIGraphicsBeginImageContext(targetSize);


CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2,
                         (targetSize.height -  image.size.height * scaleFactor) / 2,
                         image.size.width * scaleFactor, image.size.height * scaleFactor);


[image drawInRect:rect];


UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return scaledImage;
}

答案 1 :(得分:3)

试试这样: -

    UIImage *thumbnail =[UIImage imageNamed:@"yourimage.png"];
    CGSize itemSize = CGSizeMake(35, 35);
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [thumbnail drawInRect:imageRect];
   // Now this below image contains compressed image
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();