UIImage伸展的原因不明

时间:2014-03-28 20:48:45

标签: ios objective-c uiimage

编辑:这只发生在手机中。它在模拟器中工作正常。

我有一个奇怪的问题,我似乎无法纠正。

在我正在构建的应用中,您可以添加和替换个人资料照片,这就是正在发生的事情。

当您从图书馆中选择图像或使用相机拍摄新图像时,您将返回到个人资料视图,您可以看到新图像。

这是临时图像。

enter image description here

但是当我回到主导航,然后返回到个人资料页面时,图像比应该更宽。

enter image description here

我在这里真的很亏。调用每个图像的代码几乎完全相同。

// Code after initial image selection

UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];

[imageData writeToFile:savedImagePath atomically:NO];
profileImage.image = image;


//Code after returning to the page.

UIImage *image = [[UIImage alloc] initWithContentsOfFile:directoryWithProfilePicName];
profileImage.image = image;

任何想法都会很棒。

更新1:

// Creating directoryWithProfilePicName.

NSString *localDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *profilePicName = [NSString stringWithFormat:@"/%@", profilePic];  
NSString *directoryWithProfilePicName = [localDirectory stringByAppendingString:profilePicName];

// Creating the image view.

#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

if (IS_IPHONE5) {
    profileImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 335, 300)];
} else {
    profileImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 335, 210)];
}

更新2:

- (void)takeNewPhotoFromCamera {
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.sourceType = UIImagePickerControllerSourceTypeCamera;
    controller.allowsEditing = NO;
    controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
    controller.delegate = self;
    [self.navigationController presentViewController: controller animated: YES completion: nil];
}

-(void)choosePhotoFromExistingImages {
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    controller.allowsEditing = NO;
    controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
    controller.delegate = self;
    [self.navigationController presentViewController: controller animated: YES completion: nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self.navigationController dismissViewControllerAnimated: YES completion: nil];
    NSString *uuidString = [[NSUUID UUID] UUIDString];

    NSString *imageName = [NSString stringWithFormat:@"%@.jpg", uuidString];
    UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
    NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];

    NSLog(@"Pic Name: %@", imageName);
    NSLog(@"Image Size: %f x %f", image.size.width, image.size.height);
    NSLog(@"picture Taken.");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];

    [imageData writeToFile:savedImagePath atomically:NO];
    [UIView saveNewPhoto:imageName forPetID:pIDint];

    profileImage.image = image;
    profileImage.contentMode = UIViewContentModeScaleAspectFit;

}

1 个答案:

答案 0 :(得分:0)

我猜你的问题是由你的UIImageView的内容模式引起的。

theImageView.contentMode = UIViewContentModeScaleAspectFit;

确保使用ScaleAspectFit,否则您的UIImageView将默认为UIViewContentModeScaleToFill。