使用Core Data保存图像后分辨率已更改

时间:2012-04-23 04:05:38

标签: iphone ios xcode ipad core-data

我在下面有以下代码。首先,在表格详细视图中,我捕获或加载图像(图像的大小约为屏幕的1/4),然后保存后,图像将显示在UITableView上。到目前为止,一切似乎都很顺利。但是,当我按下UITable并尝试再次加载详细信息页面时。图像的分辨率降低。我猜大小与TableView的大小相同。我可以知道我能做些什么来解决这个问题吗?

感谢。

    -(void) ViewDidLoad{
       if ([currentPicture smallPicture])
                [imageField setImage:[UIImage imageWithData:[currentPicture smallPicture]]];
    }

- (IBAction)editSaveButtonPressed:(id)sender
            {

            // If we are adding a new picture (because we didnt pass one from the table) then create an entry
                if (!currentPicture)
                    self.currentPicture = (Pictures *)[NSEntityDescription insertNewObjectForEntityForName:@"Pictures" inManagedObjectContext:self.managedObjectContext];

        if (imageField.image)
        // Resize and save a smaller version for the table
                    float resize = 74.0;
                    float actualWidth = imageField.image.size.width;
                    float actualHeight = imageField.image.size.height;
                    float divBy, newWidth, newHeight;
                    if (actualWidth > actualHeight) {
                        divBy = (actualWidth / resize);
                        newWidth = resize;
                        newHeight = (actualHeight / divBy);
                    } else {
                        divBy = (actualHeight / resize);
                        newWidth = (actualWidth / divBy);
                        newHeight = resize;
                    }
                    CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
                    UIGraphicsBeginImageContext(rect.size);
                    [imageField.image drawInRect:rect];
                    UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();

                    // Save the small image version
                    NSData *smallImageData = UIImageJPEGRepresentation(smallImage, 1.0);
                    [self.currentPicture setSmallPicture:smallImageData];
                }

2 个答案:

答案 0 :(得分:2)

[在此处输入链接说明] [1]通过以下行更改此行(NSData *smallImageData = UIImageJPEGRepresentation(smallImage, 1.0); ) ..

NSData *smallImageData = UIImageJPEGRepresentation(smallImage, 0.0);

NSData *smallImageData = UIImagePNGRepresentation(smallImage);

希望,这会对你有帮助..

答案 1 :(得分:0)

(^。^)“对不起我的英语不好,如果有人喜欢纠正我的修改,我会很感激”

你试过一个UIImageView和他的属性contentMode吗? 像这样:

UIImageView *imagen = [[UIImageView alloc] initWithFrame:CGRectMake(X, Y, W, H)];
[imagen setContentMode:UIViewContentModeScaleAspectFit];