2012-07-11 19 views
15

हाय के साथ छवि सहेजें मैं एक दृश्य तो फोटो लाइब्रेरी में एक छवि के रूप में सहेजने पर कब्जा करने की कोशिश कर रहा हूँ, लेकिन मैं पर कब्जा कर लिया छवि के लिए एक कस्टम संकल्प बनाने की जरूरत है, यहाँ मेरी कोड है, लेकिन जब ऐप्लिकेशन छवियों संकल्प की बचत होती है कम है !आईओएस: कस्टम संकल्प

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 x 320 और रेटिना है: 640 x 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

+1

http://stackoverflow.com/a/613576/1059705 – Bala

+1

छवि के क्या आकार आप फोटो लाइब्रेरी से चयन करते हैं? आपके वांछित आकार से बड़ा या छोटा (1435, 1435)? – holex

उत्तर

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 रेज़ की आवश्यकता है। –

+3

जो प्रिय नहीं किया जाएगा .. आप छवि और आकार प्रदान करके ऐसा कर सकते हैं 'CGSizeMake (1425, 1435)' का उपयोग करके तय किया जाएगा। लेकिन आपकी छवि गुणवत्ता बहुत खराब होगी। –

+4

कपिल की टिप्पणी को दोहराने के लिए - प्रदर्शन में केवल 320x480 या 640x960 पिक्सल दिखाने के लिए है। आप इसे एक बड़ी छवि में प्रस्तुत कर सकते हैं, लेकिन डिस्प्ले के पिक्सल को छवि के आकार को भरने के लिए अभी बढ़ाया जाएगा। डिस्प्ले पर क्या है इसकी एक उच्च रिज़ॉल्यूशन छवि बनाने का कोई तरीका नहीं है। उस ने कहा, यदि आप डिस्प्ले फिट करने के लिए एक उच्च रेज छवि को संकुचित कर रहे हैं, या एक पीडीएफ, तो आप उन्हें एक बड़ी छवि में प्रस्तुत कर सकते हैं और उच्च रीज़ आउटपुट प्राप्त कर सकते हैं। –

0

के लिए क्या देख रहे मुझे लगता है कि ALAssetRepresentation आपकी मदद कर सकता है।

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; 

}

संबंधित मुद्दे