4

iPhone 4以降で撮影したObjective-Cの画像をダウンスケールして、できるだけ早くサーバーに送信できるようにしようとしています。現在、フルサイズの画像は古くなっています。

私が試しているダウンサイジングの現在は次のとおりです。

CGSize imageSize;

imageSize = CGSizeMake(484, 888);

UIImage *img = [UIImage imageWithContentsOfFile:path];

UIGraphicsBeginImageContext( imageSize );
[img drawInRect:CGRectMake(0,0,imageSize.width,imageSize.height)];
UIImage* realfrontImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *frontImageData = UIImageJPEGRepresentation(realfrontImage, 1.0);

これにより、320x460の画像が得られます。必要なサイズの484x888を取得するにはどうすればよいですか。

4

3 に答える 3

5
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

この方法を使用して画像を縮小します。

于 2012-04-26T13:44:35.643 に答える
0

//サイズ変更率でこの関数を呼び出します...

 -(UIImage*)scaleByRatio:(float) scaleRatio
{ 
  CGSize scaledSize = CGSizeMake(self.size.width * scaleRatio, self.size.height * scaleRatio);
   CGColorSpaceRef colorSpace;
   int   bitmapBytesPerRow;
   bitmapBytesPerRow   = (size.width * 4);

    //The output context.   
   UIGraphicsBeginImageContext(scaledSize);
   CGContextRef context = context = CGBitmapContextCreate (NULL,
                             scaledSize .width,
                             scaledSize .height,
                             8,      // bits per component
                             bitmapBytesPerRow,
                             colorSpace,
                             kCGImageAlphaPremultipliedLast);

 //Percent (101%)    
 #define SCALE_OVER_A_BIT 1.01

//Scale.
CGContextScaleCTM(context, scaleRatio * SCALE_OVER_A_BIT, scaleRatio *      SCALE_OVER_A_BIT);
[self drawAtPoint:CGPointZero];

  //End?
  UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 return scaledImage;
}

うまくいけば、これはあなたを助けるでしょう...

于 2012-04-26T13:39:16.630 に答える
0

多分この投稿はあなたを助けるでしょう:iphoneのobjective-cでプログラム的に画像のサイズを変更する方法

注意してください...これは、XCodeではなくObjective-Cで行っています。XCodeは、Objective-cを開発するアプリケーションです。NetbeansやEclipseがJavaとPHPに一般的に使用されているように。

于 2012-04-26T13:40:14.600 に答える