2012-07-11 32 views
15

您好我試圖捕獲一個視圖,然後將圖像保存爲照片庫,但我需要爲捕獲的圖像創建一個自定義分辨率,這裏是我的代碼,但是當應用程序保存圖像的分辨率低 !iOS:用自定義分辨率保存圖像

UIGraphicsBeginImageContextWithOptions(self.captureView.bounds.size, self.captureView.opaque, 0.0); 

[self.captureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); 

CGRect cropRect = CGRectMake(0 ,0 ,1435 ,1435); 
CGImageRef imageRef = CGImageCreateWithImageInRect([screenshot CGImage], cropRect); 
CGImageRelease(imageRef); 

UIImageWriteToSavedPhotosAlbum(screenshot , nil, nil, nil); 

UIGraphicsEndImageContext(); 

但在iPhone的分辨率:320×320和視網膜是:640×640

,如果你幫我解決這個問題,我將不勝感激。

+1

[SupportingHiResScreens](http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/SupportingHiResScreens/SupportingHiResScreens.html)> [以編程方式創建高分辨率位圖圖像](http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/SupportingHiResScreens/SupportingHiResScreens.html#//apple_ref/doc/uid/TP40010156-CH15- SW9) – Bala 2012-07-11 12:02:58

+1

http://stackoverflow.com/a/613576/1059705 – Bala 2012-07-11 12:09:02

+1

您從照片庫中選擇的圖像大小是多少?大於或小於你想要的尺寸(1435,1435)? – holex 2012-07-17 07:50:54

回答

15

您的代碼非常接近。你需要做的是重新渲染自定義分辨率的屏幕截圖。我修改了你的代碼來做到這一點:

UIView* captureView = self.view; 

/* Capture the screen shoot at native resolution */ 
UIGraphicsBeginImageContextWithOptions(captureView.bounds.size, captureView.opaque, 0.0); 
[captureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

/* Render the screen shot at custom resolution */ 
CGRect cropRect = CGRectMake(0 ,0 ,1435 ,1435); 
UIGraphicsBeginImageContextWithOptions(cropRect.size, captureView.opaque, 1.0f); 
[screenshot drawInRect:cropRect]; 
UIImage * customScreenShot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

/* Save to the photo album */ 
UIImageWriteToSavedPhotosAlbum(customScreenShot , nil, nil, nil); 

請注意,如果捕獲視圖不是正方形,那麼圖像將會失真。保存的圖像將始終爲正方形和1435x1435像素。

1

首先在UIImage對象中獲取圖像。創建你的尺寸,你什麼都想要和使用以下..

UIImage *image = // you image; 
CGSize size; 
if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && 
([UIScreen mainScreen].scale == 2.0)) { 

    // RETINA DISPLAY 
     size = CGSizeMake(640, 640); 
} 
else { 
    // Non Ratina device 
     size = CGSizeMake(320, 320); 
} 

UIGraphicsBeginImageContext(size); 
[image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext(); 

現在你會得到destImage用新的決議。

希望這是你在找什麼:)

+1

320和640分辨率是拍攝的圖像分辨率!我需要1435 res在視網膜和非視網膜顯示。 – 2012-07-11 12:11:07

+3

這將不會做親愛的..你可以通過提供的圖像和大小將被固定使用'CGSizeMake(1425,1435)'。但是你的圖像質量會很差。 – 2012-07-11 12:16:16

+4

只是爲了重申Kapil的評論 - 顯示屏只能顯示320x480或640x960像素。您可以將其渲染爲更大的圖像,但顯示器的像素將被拉伸以填充圖像的大小。無法爲顯示屏上的內容創建更高分辨率的圖像。也就是說,如果您顯示的高分辨率圖像縮小以適合顯示器或PDF,則可以將這些圖像渲染爲更大的圖像並獲得高分辨率的輸出。 – 2012-07-15 15:43:03

7

看看this answer。該代碼包括旋轉但仍然提問者問同樣的問題:「如何從UIImageView以其全分辨率獲取圖像?」

複製的內容(在刪除或任何的情況下):

- (UIImage *)capturedView 
{ 
    float imageScale = sqrtf(powf(self.captureView.transform.a, 2.f) + powf(self.captureView.transform.c, 2.f));  
    CGFloat widthScale = self.captureView.bounds.size.width/self.captureView.image.size.width; 
    CGFloat heightScale = self.captureView.bounds.size.height/self.captureView.image.size.height; 
    float contentScale = MIN(widthScale, heightScale); 
    float effectiveScale = imageScale * contentScale; 

    CGSize captureSize = CGSizeMake(enclosingView.bounds.size.width/effectiveScale, enclosingView.bounds.size.height/effectiveScale); 

    NSLog(@"effectiveScale = %0.2f, captureSize = %@", effectiveScale, NSStringFromCGSize(captureSize)); 

    UIGraphicsBeginImageContextWithOptions(captureSize, YES, 0.0);   
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextScaleCTM(context, 1/effectiveScale, 1/effectiveScale); 
    [enclosingView.layer renderInContext:context]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return img; 
} 
1
-(UIImage*)processImageRect:(UIImage*)image:(CGSize)sizeToForm { 
    // Draw image1 
    UIGraphicsBeginImageContext(CGSizeMake(sizeToForm.width, sizeToForm.height)); 
    [image drawInRect:CGRectMake(0.0, 0.0, sizeToForm.width, sizeToForm.height)]; 
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 

    return resultingImage; 
} 

搭配這可以解決您的問題。

1

您可以使用:

UIImageExtras.h

@interface UIImage (Extras) 
-(UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize; 
@end 

UIImageExtras.m

#import "UIImageExtras.h" 

@implementation UIImage (Extras) 

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 
{ 
//Image de base 
UIImage *sourceImage = self; 
//Image redimenssionnée 
UIImage *newImage = nil; 

//Taille de l'image de base 
CGSize imageSize = sourceImage.size; 
//Longueur et largeur 
CGFloat width = imageSize.width; 
CGFloat height = imageSize.height; 

//Dimension désirée 
CGFloat targetWidth = targetSize.width; 
CGFloat targetHeight = targetSize.height; 

//Echelle... 
CGFloat scaleFactor = 0.0; 
CGFloat scaledWidth = targetWidth; 
CGFloat scaledHeight = targetHeight; 
CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

//Si taille des image est différentes on redimensionne de facon proportionnelle 
if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
{ 
    CGFloat widthFactor = targetWidth/width; 
    CGFloat heightFactor = targetHeight/height; 

    if (widthFactor > heightFactor) 
     scaleFactor = widthFactor; // scale to fit height 
    else 
     scaleFactor = heightFactor; // scale to fit width 
    scaledWidth = width * scaleFactor; 
    scaledHeight = height * scaleFactor; 

    //Centre l'image 
    if (widthFactor > heightFactor) 
    { 
     thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
    } 
    else if (widthFactor < heightFactor) 
     { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
    }  

    UIGraphicsBeginImageContext(targetSize); 

    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 

    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    if(newImage == nil) 
     NSLog(@"could not scale image"); 

    UIGraphicsEndImageContext(); 

    return newImage; 
} 
@end 
0
CGSize sizePic = CGSizeMake(320, 460); 
    UIGraphicsBeginImageContext(sizePic); 
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *imagePic = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageWriteToSavedPhotosAlbum(imagePic, nil, nil, nil); 
0
#import <ImageIO/ImageIO.h> 
#import <MobileCoreServices/MobileCoreServices.h> 

+ (UIImage *)resizeImage:(UIImage *)image toResolution:(int)resolution { 
NSData *imageData = UIImagePNGRepresentation(image); 
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL); 
CFDictionaryRef options = (__bridge CFDictionaryRef) @{ 
                 (id) kCGImageSourceCreateThumbnailWithTransform : @YES, 
                 (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES, 
                 (id) kCGImageSourceThumbnailMaxPixelSize : @(resolution) 
                 }; 
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options); 
CFRelease(src); 
UIImage *img = [[UIImage alloc]initWithCGImage:thumbnail]; 
return img; 

}