如何以编程方式截取iphone5?

时间:2012-12-14 17:19:27

标签: xcode ios6 screenshot capture

我希望在我的应用中为图片设置一个“保存”按钮。问题是,iPhone 5和iPhone 4的图像模糊。这是我现在使用的代码。

对于iPhone 4和4s用户:

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        CGRect screenRect = [[UIScreen mainScreen] bounds];
        UIGraphicsBeginImageContext(screenRect.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [[UIColor blackColor] set];
        CGContextFillRect(ctx, screenRect);
        [self.view.layer renderInContext:ctx];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();


        CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake (0, 20, self.view.bounds.size.width, (self.view.bounds.size.height - 40)));

        //save photo to photoAlbum
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Saved!"
                                                          message:@"Image has been Saved, don't forget to Share!"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];

        UIImage *image = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        // Request to save the image to camera roll
        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);        }

对于我拥有的iPhone 5用户:

     if(result.height == 568)
    {
        CGRect screenRect = [[UIScreen mainScreen] bounds];

        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, [UIScreen mainScreen].scale);
        else
            UIGraphicsBeginImageContext(screenRect.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [[UIColor blackColor] set];
        CGContextFillRect(ctx, screenRect);
        [self.view.layer renderInContext:ctx];
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake (0, 40, self.view.bounds.size.width, (self.view.bounds.size.height - 150)));

        //save photo to photoAlbum
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Saved!"
                                                          message:@"Image has been Saved, don't forget to Share!"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
        [message show];

        UIImage *image = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);
        // Request to save the image to camera roll
        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);        }
}

2 个答案:

答案 0 :(得分:0)

也许您可以尝试使用CGContextSetShouldAntialias()设置/取消设置antialising上下文选项?

答案 1 :(得分:0)

你可能正在拍摄非视网膜320x480截图。请注意bounds的{​​{1}}是以衡量的,而非像素。